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:
- First player picks 3
- Second player picks 4
- First player picks 5
- 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 inputInput : 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