Nim Game - aloalgo

Nim Game

Medium

You are given an integer n representing the number of stones in a pile.

Your task is to determine if you can win a Nim game against a friend, assuming you are the first player.

Note that:

  • Players take turns removing 1, 2, or 3 stones from the pile.
  • The player who removes the last stone wins the game.

Return true if you can win the game, and false otherwise.

Example 1

Input
4
Output
False
Explanation:

If there are 4 stones, no matter how many stones you remove (1, 2, or 3), your friend can always remove the remaining stones to win. For example, if you remove 1, 3 stones remain, friend removes 3. If you remove 2, 2 stones remain, friend removes 2. If you remove 3, 1 stone remains, friend removes 1. In all scenarios, the first player (you) loses.

Example 2

Input
1
Output
True
Explanation:

You can remove 1 stone and win immediately.

Example 3

Input
5
Output
True
Explanation:

You can remove 1 stone, leaving 4 stones. Now, it's your friend's turn with 4 stones. As shown in the previous example, if your friend starts with 4 stones, they will lose. Thus, you win.

Loading...
Input
4
Output
False

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!