

You are given an array ‘COINS’ having ‘N’ integers corresponding to the number of coins in each sack. Your task is to find the maximum amount Ninja can collect by playing this game.
For ExampleIf the given ‘COINS’ array is [9,6,1,10], then Ninja will collect 10 and 6(Total: 16) and his friend will collect 9 and 1.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer,' N’ denoting the number of sacks in the treasure.
The next line of each test case has ‘N’ values of ‘COINS’.
Output Format:
For each test case, return an integer corresponding to the maximum coins Ninja can collect.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000.
1 <= COINS[i] <= 1000
Time limit: 1 sec
2
4
9 6 1 10
4
5 2 8 6
16
13
For the first test case,
Ninja will pick the sacks having 10 and 6 coins. So the amount of maximum coins is 16.
Hence the answer is 13.
For the second test case:
Ninja will pick 5 coins in his first turn and 8 in his second turn. So the amount of maximum coins is 13.
Hence the answer is 13.
2
7
8 3 10 3 2 8 1
5
2 1 3 4 1
21
6
Try to divide the problem into subproblems and calculate the maximum coins for smaller subarrays.
In this approach, we will use a recursive function to formulate the answer. We will define a function 'HELPER'(i, j, ’COINS’) that will find the maximum coins that can be collected for the subarray from COINS[i] to COINS[j].
Algorithm:
O(4^(N^2)), where ‘N’ is the number of sacks in the treasure.
In this approach, in the worst case, we will run the 'HELPER' function for all subarrays and in each call, we are iterating over all elements of the subarray. There are a total of N*(N-1) subarrays and for each subarray, we are making at max 4 more recursive calls. So the total time is 4^(N*(N-1)). Hence the overall time complexity is O(4^(N^2)).
O( 4^(N^2) ), where ‘N’ is the number of sacks in the treasure.
In this approach, in the worst case, we are making N^(N*N) calls of the 'HELPER'() function.I will consume stack memory. Hence the overall space complexity is O(4^(N^2))