# Number Game Algorithm

The task is to find the maximum score you can get in the game. Such problems, based on games, where you have to simulate, predict the result, or obtain the maximum possible score always seem to puzzle me.

I can do it with recursion by considering two cases - first number picked or last number picked, each of which again branches into two states similarly, and so on... which finally can yield the max possible result.

But it's a very time-inefficient approach, since time increases exponentially, due to the large test cases.

What is the most pragmatic approach to the problem, and to such problems in general?

• Hi, we aren't here to do all the work for you. What have you already tried? Nov 6, 2012 at 18:49
• Strange! I try several problems, & only post the ones here which I find difficult. As I said in the question, I have no idea on how to solve such game based questions, so I don't know where to start! Nov 6, 2012 at 18:52
• Please update your question to give us more details on your attempt to solve the problem, for example why you think your solution is time inefficient. Nov 6, 2012 at 19:27
• @YannisRizos Can I simply say that such problems stump me? :P Nov 6, 2012 at 19:32
• We're one step closer, but I want you to expand on "I can do it with recursion"... How? Nov 6, 2012 at 19:32

The trick is thus:
Consider `2,5,3,3,2,1`.

Opponent move: Pick Right
Current state: `5,3,3,2`.

vs.

Current state: `5,3,3,2`.
Using recursion, you will end up calculating how many more points you can get for `5,3,3,2` twice!
The first time you call your function on `5,3,3,2`, store the score for that (e.g., in a two dimensional array where the first index is how many numbers have been removed from the left and the second index is how many numbers have been removed from the right). The next time you are about to call it, just load the score from the previous time.