Alice and Bob Playing Flower Game - Problem
Alice and Bob are playing a strategic flower-picking game! 🌸

The game setup is elegant: there are two parallel lanes of flowers between Alice and Bob. Lane 1 has x flowers and Lane 2 has y flowers.

Game Rules:
• Alice always goes first
• On each turn, a player picks exactly one flower from either lane
• The player who picks the last flower from both lanes (making both lanes empty) wins

Your Mission: Given constraints n and m, count how many starting configurations (x, y) guarantee Alice will win with optimal play, where:
• 1 ≤ x ≤ n (flowers in lane 1)
• 1 ≤ y ≤ m (flowers in lane 2)

This is a classic game theory problem where we need to determine winning and losing positions!

Input & Output

example_1.py — Small Game
$ Input: n = 3, m = 2
› Output: 3
šŸ’” Note: Alice wins with configurations: (1,2), (2,1), (3,2). In each case, the total flowers x+y is odd (3, 3, 5 respectively), so Alice makes the final move and wins.
example_2.py — Larger Game
$ Input: n = 4, m = 4
› Output: 8
šŸ’” Note: Alice wins when x+y is odd. Pairs: (1,2), (1,4), (2,1), (2,3), (3,2), (3,4), (4,1), (4,3). Total: 8 winning configurations.
example_3.py — Edge Case
$ Input: n = 1, m = 1
› Output: 0
šŸ’” Note: Only one configuration (1,1) where x+y=2 is even. Bob wins because Alice starts but Bob makes the final move (move #2).

Visualization

Tap to expand
Alice and Bob's Flower Game StrategyšŸ‘© AliceLane 1: x flowersLane 2: y flowersBob šŸ‘ØWinning PatternAlice wins when:(x + y) is ODDExamples:(1,2)→3āœ“ (2,3)→5āœ“ (1,1)→2āœ—Why This Works?Total game length = x + y movesAlice: moves 1,3,5,7...Bob: moves 2,4,6,8...Last move is odd → Alice wins!🧮 Mathematical FormulaCount = (odd numbers in [1,n]) Ɨ (even numbers in [1,m]) +(even numbers in [1,n]) Ɨ (odd numbers in [1,m])⚔ O(1) Time ComplexityNo loops needed - pure mathematics!
Understanding the Visualization
1
Game Setup
Two lanes with x and y flowers, Alice goes first
2
Optimal Play
Both players play perfectly, making the best possible moves
3
Parity Insight
Alice wins ⟺ she makes the final move ⟺ (x+y) is odd
4
Mathematical Count
Count configurations where x+y is odd using parity formula
Key Takeaway
šŸŽÆ Key Insight: Game theory transforms a complex simulation problem into elegant mathematics - Alice wins precisely when the total flowers (x + y) is odd!

Time & Space Complexity

Time Complexity
ā±ļø
O(1)

Simple arithmetic calculation using the parity pattern

n
2n
āœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables for the calculation

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ n, m ≤ 105
  • Both n and m are positive integers
  • Time limit: 1 second per test case
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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