Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Maximum fruit gathering by birds

Easy Acceptance 50% Points 20

There are several trees arranged in a circle, each with a fruit value associated with it. A bird can gather all the fruits from a tree by sitting on it for 0.5 seconds and can move to a neighboring tree in another 0.5 seconds. Once all the fruits are picked from a tree, the bird cannot pick any more from that tree until all fruits are picked, it cannot move to next tree. The bird has totalTime seconds to gather as many fruits as possible and can start from any tree.

Given the array arr[] of fruit values and totalTime, determine the maximum number of fruits the bird can gather.

Examples
Example 1

Example 1

Input: arr[] = [2, 1, 3, 5, 0, 1, 4], totalTime = 3

Output: 9

Explanation: Starting at tree 1 and moving to tree 2, then to tree 3, the bird gathers 1 + 3 + 5 = 9 fruits.

Example 2

Example 2

Input: arr[] = [1, 6, 2, 5, 3, 4], totalTime = 2

Output: 8

Explanation: Starting at tree 1 and moving to tree 2, the bird gathers 6 + 2 = 8 fruits. Alternatively, starting at tree 3 and moving to tree 4, the bird also gathers 5 + 3 = 8 fruits.

Constraints
  • 2 = arr.size() = 10^6
  • 1 = totalTime = 10^8
  • 0 = arr[i] = 10^5
Topics
Array
Companies
Meta
Added 18 Aug 2025 50% acceptance
Ln 1, Col 1Sign in to save your code
1 6 2 5 3 4
2
8