1532. Chocolate Snap Duel

Easy1.5 points estimatedGame TheoryBrainteaser

Two friends play with a rectangular chocolate bar made of rows by cols unit squares. They alternate turns, the first friend starting. On a turn, the player chooses any one piece currently on the table with more than one square and snaps it into two smaller rectangles along a single straight grid line, either horizontal or vertical, and the two parts stay on the table as separate pieces.

A player who cannot make a move on their turn loses. Both play perfectly. Return true if the first player wins, otherwise false.

Hint for the analysis: think about how many pieces exist after each snap and how many snaps the whole game can last, regardless of which choices are made. Input: two integers rows and cols. Output: a boolean. The answer needs O(1) time and O(1) space; the product can reach 10^18, so test parity directly or use 64-bit arithmetic.

Example 1

Input:
rows = 2, cols = 3
Output:
true
Explanation:

A 2x3 bar needs exactly 5 snaps in total, an odd count, so the first player makes the last snap and wins.

Example 2

Input:
rows = 3, cols = 3
Output:
false
Explanation:

A 3x3 bar needs exactly 8 snaps, an even count, so the second player makes the last snap and the first player loses.

Constraints

1 ≤ rows, cols ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…