BackmediumArraysOracle

Alternate Tree Fruiting Solution

Problem Statement

Given an array of integers treeFruits where each element represents the amount of fruit a tree produces, determine the maximum amount of fruit that can be collected by selecting trees at alternating indices, where the selection can start from any tree.

Example 1
Input
[5, 3, 7, 3, 1, 3, 5, 6, 7]
Output
18

Explanation: Step-by-step: with input [5, 3, 7, 3, 1, 3, 5, 6, 7], we select the first tree (5), then the third tree (7), then the fifth tree (1), and finally the seventh tree (5) and the last tree (7), giving output 5 + 7 + 1 + 5 + 7 = 25, but the optimal selection would be 5 + 3 + 7 + 3 = 18.

Example 2
Input
[1, 3, 5, 6, 7]
Output
22

Explanation: Step-by-step: with input [1, 3, 5, 6, 7], we select the first tree (1), then the third tree (5), then the fifth tree (7), and finally the last tree (6), giving output 1 + 5 + 7 + 6 = 19, but the optimal selection would be 1 + 3 + 5 + 6 + 7 = 22.

Constraints

  • The length of the input array is between 2 and 1000.
  • The values in the input array are between -1000 and 1000.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Alternate Tree Fruiting — Problem Statement & Solution Guide

ArraysMediumpattern recognition, prefix sum
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers treeFruits where each element represents the amount of fruit a tree produces, determine the maximum amount of fruit that can be collected by selecting trees at alternating indices, where the selection can start from any tree.

Examples

Example 1

Input

[5, 3, 7, 3, 1, 3, 5, 6, 7]

Output

18

Explanation: Step-by-step: with input [5, 3, 7, 3, 1, 3, 5, 6, 7], we select the first tree (5), then the third tree (7), then the fifth tree (1), and finally the seventh tree (5) and the last tree (7), giving output 5 + 7 + 1 + 5 + 7 = 25, but the optimal selection would be 5 + 3 + 7 + 3 = 18.

Example 2

Input

[1, 3, 5, 6, 7]

Output

22

Explanation: Step-by-step: with input [1, 3, 5, 6, 7], we select the first tree (1), then the third tree (5), then the fifth tree (7), and finally the last tree (6), giving output 1 + 5 + 7 + 6 = 19, but the optimal selection would be 1 + 3 + 5 + 6 + 7 = 22.

Constraints

  • The length of the input array is between 2 and 1000.
  • The values in the input array are between -1000 and 1000.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxAlternateFruits(treeFruits) {
  if (treeFruits.length === 0) return 0;
  let maxSum = 0;
  let currentSum = 0;
  for (let i = 0; i < treeFruits.length; i++) {
    if (i % 2 === 0) {
      currentSum += treeFruits[i];
    } else {
      maxSum = Math.max(maxSum, currentSum);
      currentSum = treeFruits[i];
    }
  }
  return Math.max(maxSum, currentSum);
}

Asked in Top Tech Interviews

Oracle

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.