Nim Game

easy math brainteaser game theory

Problem

You and a friend take turns removing 1, 2, or 3 stones from a heap of n. The player to take the last stone wins. Both play optimally. Return true if you (going first) can win.

Inputn = 4
Outputfalse
Whatever you take (1/2/3), opponent takes the rest. Multiples of 4 are losing for the player about to move.

def can_win_nim(n):
    return n % 4 != 0
function canWinNim(n) {
  return n % 4 !== 0;
}
class Solution {
    public boolean canWinNim(int n) {
        return n % 4 != 0;
    }
}
bool canWinNim(int n) {
    return n % 4 != 0;
}
Time: O(1) Space: O(1)