Find if Digit Game Can Be Won - Problem

Welcome to an exciting number strategy game between Alice and Bob! ๐ŸŽฒ

You are given an array of positive integers nums. Alice and Bob are competing in a clever digit-based game where Alice gets to make the first strategic choice:

  • Alice can choose either all single-digit numbers (1-9) OR all double-digit numbers (10-99) from the array
  • Bob automatically gets all the remaining numbers
  • Alice wins if her sum is strictly greater than Bob's sum

Your task is to determine if Alice has a winning strategy. Return true if Alice can guarantee a win by choosing optimally, otherwise return false.

Example: If nums = [1, 2, 3, 10, 20, 30]
โ€ข Option 1: Alice takes single-digits [1,2,3] (sum=6), Bob gets [10,20,30] (sum=60) โ†’ Alice loses
โ€ข Option 2: Alice takes double-digits [10,20,30] (sum=60), Bob gets [1,2,3] (sum=6) โ†’ Alice wins!

Input & Output

example_1.py โ€” Basic Win Scenario
$ Input: nums = [1, 2, 3, 10, 20, 30]
โ€บ Output: true
๐Ÿ’ก Note: Alice can choose double-digit numbers [10, 20, 30] with sum 60, while Bob gets single-digit numbers [1, 2, 3] with sum 6. Since 60 > 6, Alice wins.
example_2.py โ€” No Win Scenario
$ Input: nums = [1, 2, 3, 4, 10, 11, 12]
โ€บ Output: false
๐Ÿ’ก Note: Single-digits sum: 1+2+3+4=10, Bob gets 33-10=23. Double-digits sum: 10+11+12=33, Bob gets 33-33=0. Alice loses with single-digits (10 < 23) but would win with double-digits (33 > 0), so Alice can win.
example_3.py โ€” Equal Distribution
$ Input: nums = [5, 5, 10, 10]
โ€บ Output: false
๐Ÿ’ก Note: Single-digits sum: 5+5=10, Bob gets 30-10=20. Double-digits sum: 10+10=20, Bob gets 30-20=10. Alice cannot win either way: 10 < 20 and 20 > 10 but she needs strictly greater, so she can win with double-digits.

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 99
  • All numbers are positive integers
  • Array contains only single-digit (1-9) and double-digit (10-99) numbers

Visualization

Tap to expand
๐ŸŽฏ Alice's Strategic Game Decision๐Ÿ’Ž Single Digits Chest[1, 2, 3]Alice gets: 6Bob gets: 26โŒ Alice loses๐Ÿ† Double Digits Chest[15, 11]Alice gets: 26Bob gets: 6โœ… Alice wins!๐ŸŽฏ Optimal Strategy Found!Alice chooses the double-digits chestResult: Alice wins with 26 > 6๐Ÿ’ก Key Algorithm Insights:1. Single Pass Efficiency: Calculate both sums simultaneously in O(n) time2. Strategic Comparison: Alice needs sum > total_sum/2 to guarantee a win3. Binary Choice: Only two possible strategies - choose the better one4. Zero-Sum Game: Alice's gain = Bob's loss, so optimize Alice's choice5. Edge Cases: Handle arrays with only single or only double-digit numbers
Understanding the Visualization
1
Separate Numbers
Sort all numbers into single-digit (1-9) and double-digit (10-99) groups
2
Calculate Group Sums
Sum up each group to see their total values
3
Evaluate Alice's Options
For each group Alice could choose, calculate what Bob would get (remaining numbers)
4
Find Winning Strategy
Alice wins if either choice gives her strictly more than Bob
5
Return Result
Return true if Alice has a winning strategy, false otherwise
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic optimization problem where Alice must choose between exactly two strategies. The algorithm efficiently calculates both possibilities in a single pass and selects the winning strategy if one exists.
Asked in
Google 45 Amazon 38 Microsoft 29 Meta 22
42.3K Views
Medium Frequency
~8 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