In the "100 game" two players take turns adding, to a running total, any integer from 1 to 10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers? For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100.

Given two integers maxChoosableInteger and desiredTotal, return true if the first player to move can force a win, otherwise, return false. Assume both players play optimally.

Input & Output

Example 1 — First Player Can Win Immediately
$ Input: maxChoosableInteger = 10, desiredTotal = 11
Output: false
💡 Note: First player cannot guarantee a win. If player 1 picks any number from 1-10, player 2 can pick a number that reaches or exceeds 11.
Example 2 — Small Numbers, Strategic Play
$ Input: maxChoosableInteger = 10, desiredTotal = 40
Output: true
💡 Note: First player can force a win by making strategic choices that put the second player in losing positions.
Example 3 — Immediate Win
$ Input: maxChoosableInteger = 5, desiredTotal = 5
Output: true
💡 Note: First player wins immediately by choosing 5, which reaches the desired total.

Constraints

  • 1 ≤ maxChoosableInteger ≤ 20
  • 0 ≤ desiredTotal ≤ 300

Visualization

Tap to expand
Can I Win - Game Theory Solution INPUT Available Numbers Pool (1 to 10) 1 2 3 4 5 6 7 8 9 10 Sum of all: 1+2+...+10 = 55 maxChoosableInteger = 10 desiredTotal = 11 Target: reach total >= 11 Goal: 11+ ALGORITHM STEPS 1 Check Base Cases If sum < target: impossible 55 >= 11 (OK, proceed) 2 Use Bitmask for State Track used numbers mask: 0000000000 (10 bits) 3 Memoized Recursion Try each unused number Check if opponent loses 4 Optimal Play Logic Win if ANY move makes opponent unable to win Game Tree Analysis: P1 picks i P2 turn P2 turn FINAL RESULT Analysis for this case: P1 picks any number i P2 picks (11-i) if available P2 wins immediately! (Both 1-10, complementary pairs exist) Example Scenarios: P1: picks 5 P2: picks 6 5+6=11 P2 wins P1: picks 10 P2: picks 1 10+1=11 P2 wins Output: false Player 1 CANNOT force a win with optimal play Key Insight: This problem uses Game Theory with Memoization. The key is that for target=11 with numbers 1-10, Player 2 can always respond with the complement to 11 (e.g., if P1 picks 3, P2 picks 8). State is tracked via bitmask (2^n states). Time: O(2^n * n), Space: O(2^n) for memoization. TutorialsPoint - Can I Win | Optimal Game Theory with Memoization
Asked in
Google 25 Microsoft 18 Amazon 15
98.6K Views
Medium Frequency
~35 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