Ctrl K

Two Piles and the Last Stone

Medium
Lena and Max play with two piles of stones: the left one has x stones and the right one has y. They take turns, and Lena moves first. On each turn a player must take exactly one stone from either non-empty pile. Whoever takes the last stone wins. For how many pairs (x, y) with 1 ≤ x ≤ max_left and 1 ≤ y ≤ max_right does Lena win? Return that count.
Example 1
max_left=2, max_right=3 → 3

The pairs (1, 2), (2, 1) and (2, 3) work: each has an odd total, so Lena takes the last stone.

Example 2
max_left=1, max_right=1 → 0

The only pair is (1, 1): Lena takes one stone, Max takes the last one and wins.

Example 3
max_left=4, max_right=4 → 8
Constraints
  • 1 ≤ max_left, max_right ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
max_left=2, max_right=3
Expected
3

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