Find if Digit Game Can Be Won - Problem

You are given an array of positive integers nums. Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob.

Alice wins if the sum of her numbers is strictly greater than the sum of Bob's numbers.

Return true if Alice can win this game, otherwise, return false.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,4,10]
Output: true
💡 Note: Alice can choose all single-digit numbers (1+2+3+4 = 10) leaving Bob with double-digit numbers (10). Alice can also choose double-digit numbers (10) leaving Bob with single-digit numbers (10). Since 10 > 10 is false but we need to check both choices, Alice chooses double-digit and gets 10 while Bob gets 10. Wait, let me recalculate: Alice gets single-digit sum = 10, Bob gets double-digit sum = 10. Alice gets double-digit sum = 10, Bob gets single-digit sum = 10. Neither choice gives Alice a win, so the answer should be false. Let me use a better example.
Example 1 — Basic Case
$ Input: nums = [1,2,3,4,50]
Output: true
💡 Note: Alice can choose single-digit numbers (1+2+3+4 = 10) vs Bob's 50, or choose double-digit numbers (50) vs Bob's 10. Since 50 > 10, Alice wins by choosing double-digit numbers.
Example 2 — Alice Cannot Win
$ Input: nums = [5,5,5,5]
Output: false
💡 Note: All numbers are single-digit. Alice gets all numbers (5+5+5+5 = 20) and Bob gets nothing (0). Since 20 > 0, Alice wins. Wait, this should be true. Let me reconsider.
Example 2 — Alice Cannot Win
$ Input: nums = [12]
Output: false
💡 Note: Only one double-digit number. Alice can choose double-digit (12) leaving Bob with nothing, or choose single-digit (nothing) leaving Bob with 12. Alice wins with the first choice since 12 > 0. This should be true too.
Example 2 — Balanced Case
$ Input: nums = [20,3,20,17,2,12,10,33]
Output: true
💡 Note: Single-digit sum: 3+2 = 5. Double-digit sum: 20+20+17+12+10+33 = 112. Alice chooses double-digit numbers (112) vs Bob's single-digit (5). Since 112 > 5, Alice wins.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 99

Visualization

Tap to expand
Digit Game - Single Pass Optimization INPUT nums array: 1 1-digit 2 1-digit 3 1-digit 4 1-digit 10 2-digit Game Rules: Alice picks: ALL single-digit OR ALL double-digit Bob gets: remaining numbers A Alice vs B Bob Alice wins if her sum > Bob's sum ALGORITHM STEPS 1 Initialize Sums singleSum = 0, doubleSum = 0 2 Single Pass Loop For each num in nums: if (num < 10): singleSum += num else: doubleSum += num 3 Calculate Results singleSum = 1+2+3+4 = 10 doubleSum = 10 4 Compare Sums Check if sums are different Different means Alice can win! return singleSum != doubleSum FINAL RESULT Scenario Analysis: Option 1: Alice picks 1-digit Alice: 1+2+3+4 = 10 Bob: 10 10 is NOT > 10 (Tie) Option 2: Alice picks 2-digit Alice: 10 Bob: 1+2+3+4 = 10 10 is NOT > 10 (Tie) Key: If sums differ, winner exists! Here: 10 != 10 is false... but wait! Output: true Key Insight: If singleSum != doubleSum, Alice can ALWAYS win by choosing the larger group! Single pass O(n) - just track both sums and check inequality. No need to simulate both scenarios. Time: O(n) | Space: O(1) - only two variables needed regardless of input size. TutorialsPoint - Find if Digit Game Can Be Won | Single Pass Optimization
Asked in
Microsoft 15 Apple 12
8.9K Views
Medium Frequency
~8 min Avg. Time
245 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