


1. Each coin has a value associated with it.
2. It’s a two-player game played against an opponent with alternating turns.
3. At each turn, the player picks either the first or the last coin from the line and permanently removes it.
4. The value associated with the coin picked by the player adds up to the total amount the player wins.
'N' is always even number.
Ninjax is as smart as you, so he will play so as to maximize the amount he wins.
Let the values associated with four coins be: [9, 5, 21, 7]
Let’s say that initially, you pick 9 and Ninjax picks 7.
Then, you pick 21 and Ninjax picks 5.
So, you win a total amount of (9+21), i.e. 30.
In case you would have picked up 7 initially and Ninjax would have picked 21 (as he plays optimally).
Then, you would pick 9 and Ninjax would choose 5. So, you win a total amount of (7+9), i.e. 16, which is not the maximum you can obtain.
Thus, the maximum amount you can win is 30.
Let the values associated with four coins be: [20, 50, 5, 10]
Let’s say that initially, you pick 10 and Ninjax picks 20.
Then, you pick 50 and Ninjax picks 5.
So, you win a total amount of (10+50), i.e. 60.
In case you would have picked up 20 initially and Ninjax would have picked 50 (as he plays optimally).
Then, you would pick 10 and Ninjax would choose 5. So, you win a total amount of (20+10), i.e. 30, which is not the maximum you can obtain.
Thus, the maximum amount you can win is 60.
The very first line of input contains an integer T denoting the number of test cases.
The first line of every test case contains an integer ‘N’ denoting the number of coins present in the line initially.
The second line of every test case contains ‘N’ space-separated integers denoting the values associated with the coins placed by Ninjax.
For each test case, print the required answer 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' <= 10
2 <= 'N' <= 10 ^ 3
0 <= 'VALUE' <= 10 ^ 5
Where 'T' is the number of test cases, 'N' is the number of coins and 'VALUE' is the amount on each coin.
Time Limit: 1 sec
Algorithm for the above solution is as follows:
→ Int helper(vector<int> ‘COINS’, int ‘I’, int ‘J'):
→ Int optimalStrategyOfGame(vector<int> ‘COINS’, int ‘N'):
In the previous approach, we were making 4 recursive calls every time we called the helper function but a better approach to solve, would be as follows:
The algorithm for the helper function would be -
→ Int helper(vector<int> ‘COINS’, int 'I', int 'J', int ‘SUM’):
Approach 1 solves the same sub-problems multiple times i.e. helper function gets called for the same sub-array (index range) multiple times.
The algorithm for the helper function is as follows:
Int helper(vector<int> ‘COINS’, int ‘I’, int 'J', int SUM):
The idea behind the approach will be:
Suppose it's your turn and you are left with coins in the index range ['I', ‘J’] (other coins have already been picked up in previous turns). You have the option to pick ith or jth coin. Of these two options, you would select the one which maximizes your winning amount.
Let us understand the above algorithm in detail with an example:
For the test case where ‘N’ = 4, Coins List = [5, 40, 4, 1]
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers