-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCoinGameWinner.js
45 lines (42 loc) · 1.35 KB
/
CoinGameWinner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @author Anirudh Sharma
* A and B are playing a game. At the beginning there are n coins. Given two more numbers x and y.
* In each move a player can pick x or y or 1 coins. A always starts the game.
* The player who picks the last coin wins the game or the person who is not able to pick any
* coin loses the game.
* For a given value of n, find whether A will win the game or not if both are playing optimally.
*/
const findWinner = (x, y, n) => {
// Lookup table to store the results for different
// values of n
const lookup = new Array(n + 1).fill(false);
// Initial values
// A cannot pick up any coin
lookup[0] = false;
// A can pick up the one and only coin
lookup[1] = true;
// Populate the remaining values
for (let i = 2; i <= n; i++) {
// If A loses any of i - 1 or i - x or i - y game,
// then it will definitely win game i
if (i - 1 >= 0 && !lookup[i - 1]) {
lookup[i] = true;
} else if (i - x >= 0 && !lookup[i - x]) {
lookup[i - x] = true;
} else if (i - y >= 0 && !lookup[i - y]) {
lookup[i - y] = true;
}
}
return lookup[n];
};
const main = () => {
let n = 5;
let x = 3;
let y = 4;
console.log(findWinner(x, y, n));
n = 2;
x = 3;
y = 4;
console.log(findWinner(x, y, n));
};
main();