Categories

# Find the winner

Lets say you want to find the winner of a game where the participants pick up stones (n). If ‘A’ and ‘B’ are the 2 players, then the rules of the game are as follows:

• Player ‘A’ always starts first.
• Player ‘A’ always picks 1 stone.
• Player ‘B’ always picks 2 stones.
• Player ‘A’ and Player ‘B’ take alternate turns.
• The player to pick up the last stone is the loser.

Write a method that returns the winner. There can be 3 possible outcomes “A | B | null”.

``````/**
* @param {number} n
* @return {'A' | 'B' | null}
*/
function findWinner(n) {
// A : true
// B : false
if (!n || n <= 0)
return null;
return whoWins(n - 1, true) ? 'A' : 'B';
}

function whoWins(n, player) {
if (n <= 0)
return !player;
return whoWins(n - (player ? 2 : 1), !player);
}

/** Test cases **/
console.log(findWinner(1)); // B
console.log(findWinner(2)); // A
console.log(findWinner(3)); // A
console.log(findWinner(4)); // B
console.log(findWinner(0)); // null
console.log(findWinner(null)); // null

``````

Demo

A shorter version of the above code in ES6.

``````const findWinner = (n) => (!n || n <= 0) ? null : whoWins(n - 1, true) ? 'A' : 'B';
const whoWins = (n, player) => (n <= 0) ? !player : whoWins(n - (player ? 2 : 1), !player);

/** Test cases **/
console.log(findWinner(1)); // B
console.log(findWinner(2)); // A
console.log(findWinner(3)); // A
console.log(findWinner(4)); // B
console.log(findWinner(0)); // null
console.log(findWinner(null)); // null``````

Demo

Iterative Solution

``````const findWinner = (n) => {
if (!n || n < 1) {
return null;
}
let totalStones = 0;
let player = 1; // A
while (totalStones < n) {
totalStones += player ? 1 : 2;
player = !player;
}
return player ? 'A' : 'B';
};``````

Demo

Using for loop

``````const findWinner = (n) => {
if (!n || n < 1) {
return null;
}
let player = 1; // A
for (let totalStones = 0; totalStones < n; totalStones += player ? 1 : 2) {
player = !player;
}
return player ? 'A' : 'B';
};

/** Test cases **/
console.log(findWinner(1));      // B
console.log(findWinner(2));      // A
console.log(findWinner(3));      // A
console.log(findWinner(4));      // B
console.log(findWinner(0));      // null
console.log(findWinner(null));   // null``````

Demo