Sum Game - Problem
Sum Game is a strategic two-player game where Alice and Bob compete to control the outcome of a mathematical equation.
You're given a string
1. Choose an index
2. Replace it with any digit from
The game ends when all
Example: If the final string is
Assuming both players play optimally, determine if Alice will win.
You're given a string
num of even length containing digits and '?' characters. Alice starts first, and players alternate turns. On each turn, a player must:1. Choose an index
i where num[i] == '?'2. Replace it with any digit from
'0' to '9'The game ends when all
'?' characters are filled. Bob wins if the sum of digits in the first half equals the sum of digits in the second half. Alice wins if the sums are different.Example: If the final string is
"243801", Bob wins because 2+4+3 = 8+0+1 = 9. If it's "243803", Alice wins because 2+4+3 โ 8+0+3.Assuming both players play optimally, determine if Alice will win.
Input & Output
example_1.py โ Basic case
$
Input:
num = "5023"
โบ
Output:
false
๐ก Note:
No question marks exist, so the game ends immediately. Left sum = 5+0 = 5, Right sum = 2+3 = 5. Since the sums are equal, Bob wins, so Alice does not win (return false).
example_2.py โ Alice advantage
$
Input:
num = "25??"
โบ
Output:
true
๐ก Note:
Left sum = 2+5 = 7, Right sum = 0 (both are ?). Alice goes first and will place a small digit (0) in the right half. Bob must place a digit, but Alice's second move can ensure the sums remain unequal. Alice wins.
example_3.py โ Balanced scenario
$
Input:
num = "?3?2"
โบ
Output:
true
๐ก Note:
Left: ?+3, Right: ?+2. Current difference is 1 (3-2). With optimal play, Alice can prevent Bob from achieving perfect balance by strategically choosing her digits.
Constraints
- 2 โค num.length โค 105
- num.length is even
-
num consists of digits and
'?'characters only
Visualization
Tap to expand
Understanding the Visualization
1
Initial Assessment
Examine the current state: sum each half and count the question marks
2
Calculate Advantage
Determine the current imbalance and how question marks are distributed
3
Optimal Strategy
Alice maximizes differences, Bob minimizes them - both play optimally
4
Mathematical Formula
Apply the game theory formula to predict the outcome instantly
Key Takeaway
๐ฏ Key Insight: Game theory allows us to predict optimal play outcomes through mathematical analysis rather than expensive simulation, turning an exponential problem into a linear one.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code