Find the Winning Player in Coin Game - Problem

You are given two positive integers x and y, denoting the number of coins with values 75 and 10 respectively.

Alice and Bob are playing a game. Each turn, starting with Alice, the player must pick up coins with a total value of exactly 115. If the player is unable to do so, they lose the game.

Return the name of the player who wins the game if both players play optimally.

Input & Output

Example 1 — Basic Case
$ Input: x = 2, y = 8
Output: Bob
💡 Note: Maximum turns = min(2, 8÷4) = min(2, 2) = 2. Since 2 is even, Bob wins.
Example 2 — Alice Wins
$ Input: x = 4, y = 11
Output: Bob
💡 Note: Maximum turns = min(4, 11÷4) = min(4, 2) = 2. Since 2 is even, Bob wins.
Example 3 — Insufficient Coins
$ Input: x = 1, y = 3
Output: Bob
💡 Note: Need 1×75 + 4×10 = 115, but only have 3 ten-coins (need 4). Alice can't make first move, so Bob wins.

Constraints

  • 1 ≤ x, y ≤ 100

Visualization

Tap to expand
Find the Winning Player in Coin Game INPUT Coins Available 75-value coins (x=2) 75 75 10-value coins (y=8) 10 10 10 10 10 10 10 10 Input Values x = 2 (75-coins) y = 8 (10-coins) ALGORITHM STEPS 1 Find Valid Combination 75 + 4x10 = 115 2 Count Max Turns turns = min(x, y/4) 3 Calculate min(2, 8/4) = min(2,2) = 2 4 Determine Winner If turns is odd: Alice If turns is even: Bob Game Simulation Turn Player Picks 1 Alice 75+4x10 2 Bob 75+4x10 3 Alice Can't play! FINAL RESULT After 2 complete turns: Remaining Coins 75-coins: 0 10-coins: 0 WINNER Bob Output "Bob" turns=2 is even, Bob wins Key Insight: Each valid turn requires exactly 1 coin of 75 and 4 coins of 10 (75 + 40 = 115). Maximum turns possible = min(x, y/4). If this count is odd, Alice (first player) wins; if even, Bob wins. With x=2, y=8: min(2, 2)=2 turns (even), so Bob wins! TutorialsPoint - Find the Winning Player in Coin Game | Optimal Solution
Asked in
Google 15 Amazon 12
12.0K Views
Medium Frequency
~8 min Avg. Time
450 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