Alice and Bob Playing Flower Game - Problem

Alice and Bob are playing a turn-based game on a field with two lanes of flowers between them. There are x flowers in the first lane and y flowers in the second lane.

Game Rules:

  • Alice takes the first turn
  • In each turn, a player must choose one lane and pick exactly one flower from that lane
  • The player who removes the last flower (making both lanes empty) wins the game

Given two integers n and m, count the number of pairs (x, y) where:

  • Alice wins the game with optimal play
  • 1 ≤ x ≤ n
  • 1 ≤ y ≤ m

Input & Output

Example 1 — Small Grid
$ Input: n = 3, m = 2
Output: 3
💡 Note: Alice wins for pairs: (1,2), (2,1), (3,2). These have odd sums: 3, 3, 5 respectively.
Example 2 — Symmetric Case
$ Input: n = 5, m = 4
Output: 10
💡 Note: Odd numbers 1-5: {1,3,5} (3 values), Even numbers 1-4: {2,4} (2 values). Total: 3×2 + 2×2 = 10.
Example 3 — Minimum Case
$ Input: n = 1, m = 1
Output: 0
💡 Note: Only pair (1,1) has sum 2 (even), so Bob wins. Alice wins in 0 cases.

Constraints

  • 1 ≤ n, m ≤ 105

Visualization

Tap to expand
Alice and Bob Flower Game: Input n=3, m=2Alice🌸🌸🌸Lane 1 (x flowers)🌺🌺Lane 2 (y flowers)BobWho wins each (x,y) pair?(1,2)→3 odd(2,1)→3 odd(1,1)→2 even(2,2)→4 even(3,1)→4 odd(3,2)→5 oddAlice wins 3 pairs: (1,2), (2,1), (3,2)■ Alice Wins (Odd Sum)■ Bob Wins (Even Sum)
Understanding the Visualization
1
Game Setup
Two lanes with x and y flowers
2
Key Insight
Alice wins when x+y is odd
3
Count Winners
Calculate odd/even combinations
Key Takeaway
🎯 Key Insight: Alice wins exactly when the total flowers (x+y) is odd
Asked in
Google 15 Microsoft 12
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen