
All trees are in a circle.
Input: N = 7 M = 3
ARR[] = { 2, 1, 3, 5, 0, 1, 4 }
Output: 9
Explanation:
She can start from tree 1 and move to tree 2 and then to tree 3.
Hence, total number of gathered fruits = 1 + 3 + 5 = 9.
The first line of input contains a single integer ‘T’ denoting the number of test cases for the problem.
The first line of each test case in the input contains two space-separated integers ‘N’ and ‘M’ denoting the number of trees and the seconds given to collect the fruits from the trees.
And the second line contains 'N' single space-separated integers, denoting the fruits on each tree.
For each test case, print a single line containing a single integer denoting the maximum number of fruits the ninja bird can gather in the given amount of time.
The output of each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= M <= 5000
1 <= N <= 5000
1 <= ARR[i] <= 5000
Time Limit: 1 sec.
We will iterate through the trees with the help of a nested loop. Where we will further loop for the given number of seconds ‘m’, checking for every sum that we obtain is the maximum sum or not and update the sum on each iteration if it is maximum.
The algorithm will be-
The main observation here is that we now only need to find out the maximum sum of values of a window of size M (in a circular array of size N) as she has only M seconds of time with the ninja bird.
The algorithm will be -