Ctrl K

XOR Tile Duel

Hard
Tiles with numbers tiles lie on a table. Anna and Boris take turns removing exactly one tile, Anna first. Keep track of the bitwise XOR of the numbers still on the table (the XOR of a single tile is its number, and the XOR of an empty table is 0): - if the XOR of the remaining tiles is 0 at the start of a player's turn, that player wins at once; - if a player's move makes the XOR of the remaining tiles equal to 0, that player loses. Both play optimally. Return True if Anna wins and False otherwise.
Example 1
tiles=[4, 4, 6] → False

If Anna removes 6, [4, 4] is left with XOR 0 and she has lost. If she removes a 4, [4, 6] is left with XOR 2; Boris removes either tile, and Anna has to remove the last one, bringing the XOR to 0. Anna loses.

Example 2
tiles=[5, 9] → True

Whichever tile Anna removes, Boris is left with one nonzero tile, and removing it brings the XOR to 0. Anna wins.

Example 3
tiles=[3, 5, 6] → True

3 XOR 5 XOR 6 = 0 before any move, so Anna wins straight away.

Constraints
  • 1 ≤ len(tiles) ≤ 1000
  • 0 ≤ tiles[i] < 2¹⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTake From Any Pile MediumKnight Versus Pawns HardOR of All Subset Sums MediumCheckerboard by Swapping Lines HardTwo Groups With the Same Average Hard
loading Python…
Input
tiles=[4, 4, 6]
Expected
False

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