Maximal Array Game

Given a list of n numbers. Two players play a game. Player 1 makes the first move, and on each move:

  • A player removes either the first or last number from the list.
  • The removed number is added to that player's score.

The Task is to find the maximum possible score for the first player if both players play optimally

Example 1:

Input : n = 4 , arr = [4,5,1,3]

Output : 8

Explanation : Optimal sequence:

  1. First player picks 3
  2. Second player picks 4
  3. First player picks 5
  4. Second player picks 1

Scores:

Player1 = 3 + 5 = 8
Player2 = 4 + 1 = 5

So the maximum score for player 1 is 8

Example 2:

Input : n = 3 , arr = [2,9,1]

Output : 3

Explanation : Possible choices:

If player picks 1 first:

1 + 2 = 3

Opponent gets 9.

So the maximum score player1 can guarantee is 3.

Now Your Turn!

Pick the correct output for the given input

Input : n = 5 , arr = [3,9,1,2,7]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n ≤ 5000
  • -109 ≤ xi ≤ 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long removalGame(int n, vector<long long>& arr) {
// Write your code here
return 0;
}
};
Test Case

Input:

Arr