Ctrl K

Never a Multiple of Three

Medium
Tokens lie on a table, and token i carries the number tokens[i]. Two players take turns removing one token of their choice, the first player moving first. Every removed token goes into a shared pile. A player whose move makes the sum of the shared pile a multiple of 3 loses immediately. If the tokens run out and that never happened, the second player wins, even if it would be the first player's turn. Both play optimally. Return True if the first player wins and False otherwise.
Example 1
tokens=[4, 5] → True

The first player takes either token, the second is left with the other one, and the sum becomes 9, a multiple of 3, so the second player loses.

Example 2
tokens=[7] → False

The first player takes the only token; the sum 7 is not a multiple of 3, but the tokens have run out, so the second player wins.

Example 3
tokens=[3, 6, 1] → False
Constraints
  • 1 ≤ len(tokens) ≤ 10⁵
  • 1 ≤ tokens[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Middle Bag MediumSame Prizes, Different Tastes MediumWord Topping Duel HardDuel for the End Cards MediumJobs With a Cooldown Medium
loading Python…
Input
tokens=[4, 5]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.