> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/optimal-strategy-for-a-game.md).

# Optimal Strategy for a Game

Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.

Note: The opponent is as clever as the user.

Let us understand the problem with few examples:

1. 5, 3, 7, 10 : The user collects maximum value as 15(10 + 5)
2. 8, 15, 3, 7 : The user collects maximum value as 22(7 + 15)

Does choosing the best at each move gives an optimal solution? No.\
In the second example, this is how the game can be finished:

1. …….User chooses 8.\
   …….Opponent chooses 15.\
   …….User chooses 7.\
   …….Opponent chooses 3.\
   Total value collected by user is 15(8 + 7)
2. …….User chooses 7.\
   …….Opponent chooses 8.\
   …….User chooses 15.\
   …….Opponent chooses 3.\
   Total value collected by user is 22(7 + 15)

So if the user follows the second game state, the maximum value can be collected although the first move is not the best.

```java
class Solution {
    public static int optimalStrategyOfGame(int arr[], int n) {
        // dp[i][j] stores 2 values , 1st -> max possible values for 1st player ,
        // 2nd -> max possible values for second player
        int[][][] dp = new int[n][n][];
        // Base case when i=j , i.e -> only once coin is considered
        for (int i = 0; i < n; i++) {
            dp[i][i] = new int[]{arr[i], 0};
        }
        for (int k = 1; k < n; k++) {
            for (int i = 0; i + k < n; i++) {
                int j = i + k;

                int pickLeftScore = arr[i] + dp[i + 1][j][1];

                int pickRightScore = arr[j] + dp[i][j - 1][1];

                if (pickLeftScore > pickRightScore) {
                    int secondPlayerScore = dp[i + 1][j][0];
                    dp[i][j] = new int[]{pickLeftScore, secondPlayerScore};
                } else {
                    int secondPlayerScore = dp[i][j - 1][0];
                    dp[i][j] = new int[]{pickRightScore, secondPlayerScore};
                }
            }
        }
        return dp[0][n - 1][0];
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/optimal-strategy-for-a-game.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
