Problem Statement

We are given:

An array arr[] containing the fruit values of trees.

The trees are arranged in a circle, so the first and last trees are also neighbors.

An integer m, representing the maximum number of trees the bird can visit.

The bird can start at any tree and move to neighboring trees. We need to find the maximum total fruit value the bird can collect by visiting at most m trees.

Example

Code
arr = [2, 1, 3, 5, 0, 1, 4]
m = 3

The best choice is:

Code
1 + 3 + 5 = 9

Therefore:

Code
Answer = 9

Key Observation

Since the trees are arranged in a circle, the bird always visits consecutive trees.

For example:

Code
[2, 1, 3, 5, 0, 1, 4]

If m = 3, possible groups include:

Code
2, 1, 3
1, 3, 5
3, 5, 0
5, 0, 1
0, 1, 4
1, 4, 2 ← circular
4, 2, 1 ← circular

So the problem becomes:

Find the maximum sum of m consecutive elements in a circular array.

Because every fruit value is non-negative, using the maximum allowed number of trees is always optimal. Therefore, we use:

Java
k = Math.min(m, n);

If m > n, the bird cannot visit more than n distinct trees, so we consider the entire array.

Sliding Window Technique

A brute-force solution would calculate the sum of every possible group of m trees.

That could take:

Code
O(n × m)

which is too slow when n and m can be as large as 10^6.

Instead, we use a sliding window.

Suppose:

Code
arr = [2, 1, 3, 5, 0, 1, 4]
m = 3

First calculate:

Code
2 + 1 + 3 = 6

Now move the window one position:

Code
[2, 1, 3] → [1, 3, 5]

Instead of calculating 1 + 3 + 5 from scratch:

Code
old sum = 6
remove 2
add 5
new sum = 6 - 2 + 5
= 9

This takes constant time.

Handling the Circular Array

This is the most important part of the problem.

Consider:

Code
arr = [7, 2, 1, 3, 4]

For m = 2, the normal windows are:

Code
7 + 2
2 + 1
1 + 3
3 + 4

But because the array is circular, we also need:

Code
4 + 7

We can handle this without creating another array.

We use:

Code
i % n

For example, if:

Code
n = 5

then:

Code
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1

So after reaching the last element, % n automatically takes us back to the beginning.

Complete Java Code

Java
import java.util.*;

class Solution {
    public int maxFruits(ArrayList<Integer> arr, int m) {
        int n = arr.size();

        // We cannot visit more than n trees
        int k = Math.min(m, n);

        // Calculate the first window
        int windowSum = 0;

        for (int i = 0; i < k; i++) {
            windowSum += arr.get(i);
        }

        int maxSum = windowSum;

        // Slide the window around the circular array
        for (int i = k; i < n + k - 1; i++) {
            // Remove the element leaving the window
            windowSum -= arr.get((i - k) % n);

            // Add the new element entering the window
            windowSum += arr.get(i % n);

            maxSum = Math.max(maxSum, windowSum);
        }

        return maxSum;
    }
}

Line-by-Line Explanation

1. Get the Array Size

Java
int n = arr.size();

If:

Code
arr = [2, 1, 3, 5, 0, 1, 4]

then:

Code
n = 7

2. Determine the Window Size

Java
int k = Math.min(m, n);

Suppose:

Code
n = 7
m = 3

then:

Code
k = 3

If:

Code
n = 7
m = 10

then:

Code
k = 7

because there are only 7 trees.

3. Calculate the First Window

Java
int windowSum = 0;

for (int i = 0; i < k; i++) {
    windowSum += arr.get(i);
}

For:

Code
arr = [2, 1, 3, 5, 0, 1, 4]
k = 3

we calculate:

Code
2 + 1 + 3 = 6

So:

Code
windowSum = 6

4. Store the Maximum

Java
int maxSum = windowSum;

Initially:

Code
maxSum = 6

5. Slide the Window

Java
for (int i = k; i < n + k - 1; i++)

This allows us to check all possible circular windows.

For each new window, two things happen:

Java
windowSum -= arr.get((i - k) % n);

Remove the old element.

Then:

Java
windowSum += arr.get(i % n);

Add the new element.

Dry Run

Let's use:

Code
arr = [2, 1, 3, 5, 0, 1, 4]
m = 3

Initial window:

Code
[2, 1, 3]
sum = 6
max = 6

Window 2

Remove 2, add 5:

Code
[1, 3, 5]
Code
sum = 6 - 2 + 5
= 9
Code
max = 9

Window 3

Remove 1, add 0:

Code
[3, 5, 0]
Code
sum = 9 - 1 + 0
= 8

Window 4

Code
[5, 0, 1]
Code
sum = 8 - 3 + 1
= 6

Window 5

Code
[0, 1, 4]
Code
sum = 6 - 5 + 4
= 5

Circular Window 6

Now we need:

Code
[1, 4, 2]

The 2 comes from the beginning of the array.

Because:

Code
i % n

wraps the index around.

Sum:

Code
1 + 4 + 2 = 7

Circular Window 7

Code
[4, 2, 1]
Code
sum = 4 + 2 + 1
= 7

Therefore:

Code
maximum = 9

Why Is the Complexity O(n)?

We calculate the first window in O(m) time.

Then we slide the window around the array. Each step takes O(1) time because we only:

  • Remove one element.

  • Add one element.

  • Compare with the maximum.

Overall:

Code
O(m) + O(n)

which is:

Code
O(n)

The algorithm uses only a few variables:

Code
n
k
windowSum
maxSum
i

Therefore:

Code
Auxiliary Space = O(1)

Complexity

Metric

Complexity

Time

O(n)

Auxiliary Space

O(1)

This satisfies the expected complexity of the problem.

Important Note About int

With the stated constraints:

Code
arr[i] <= 10^6
n <= 10^6

the total can theoretically reach:

Code
10^6 × 10^6 = 10^12

which is larger than Java's int range.

So if the platform allows a long return type, the safer implementation is to use long for windowSum and maxSum. If the platform requires exactly:

Java
public int maxFruits(ArrayList<Integer> arr, int m)

then keep the int signature as given by the problem.