3232. Find if Digit Game Can Be Won
Problem Description
You have an array nums
containing only positive integers. Alice and Bob are playing a game with these numbers.
The game rules are:
- Alice must choose either all single-digit numbers (1-9) or all double-digit numbers (10-99) from the array
- Bob gets all the remaining numbers that Alice didn't choose
- Alice wins if the sum of her chosen numbers is strictly greater than the sum of Bob's numbers
Your task is to determine if Alice can win this game. Return true
if Alice has a winning strategy, otherwise return false
.
For example, if nums = [1, 2, 3, 4, 10, 11]
:
- Alice could choose all single-digit numbers:
[1, 2, 3, 4]
with sum = 10 - Bob would get the double-digit numbers:
[10, 11]
with sum = 21 - Since 10 < 21, Alice loses with this choice
- Alternatively, Alice could choose all double-digit numbers:
[10, 11]
with sum = 21 - Bob would get the single-digit numbers:
[1, 2, 3, 4]
with sum = 10 - Since 21 > 10, Alice wins with this choice
The solution recognizes that Alice can win as long as the sum of single-digit numbers is different from the sum of double-digit numbers. When these sums are different, Alice will simply choose whichever group has the larger sum to guarantee her victory.
Intuition
Let's think about what Alice's options are. She has exactly two choices:
- Take all single-digit numbers
- Take all double-digit numbers
Since Alice gets to choose first and she wants to win, she will naturally pick whichever option gives her the larger sum.
Let's denote:
a
= sum of all single-digit numbersb
= sum of all double-digit numbers
If Alice chooses single-digit numbers, she gets a
and Bob gets b
. Alice wins if a > b
.
If Alice chooses double-digit numbers, she gets b
and Bob gets a
. Alice wins if b > a
.
The key insight is that Alice can win if and only if a ≠ b
. Why?
- If
a > b
, Alice chooses single-digit numbers and wins - If
b > a
, Alice chooses double-digit numbers and wins - If
a = b
, no matter what Alice chooses, she gets the same sum as Bob, resulting in a tie (not a win)
Since Alice needs to win strictly (her sum must be greater, not equal), she cannot win when a = b
.
This leads us to the elegant solution: simply calculate the sum of single-digit numbers and the sum of double-digit numbers. If they're different, Alice can win by choosing the larger group. If they're equal, Alice cannot win.
Learn more about Math patterns.
Solution Approach
The implementation is straightforward based on our intuition. We need to:
- Calculate the sum of all single-digit numbers (numbers less than 10)
- Calculate the sum of all double-digit numbers (numbers greater than or equal to 10)
- Check if these two sums are different
Here's how the solution works:
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
a = sum(x for x in nums if x < 10)
b = sum(x for x in nums if x > 9)
return a != b
Step-by-step breakdown:
-
Calculate sum of single-digit numbers:
a = sum(x for x in nums if x < 10)
- We use a generator expression with list comprehension to filter numbers less than 10
- The
sum()
function adds up all these single-digit numbers
-
Calculate sum of double-digit numbers:
b = sum(x for x in nums if x > 9)
- Similarly, we filter numbers greater than 9 (which are the double-digit numbers)
- Sum all these double-digit numbers
-
Determine if Alice can win:
return a != b
- If the sums are different (
a != b
), Alice can choose the larger group and win - If the sums are equal (
a == b
), Alice cannot win regardless of her choice
- If the sums are different (
Time Complexity: O(n)
where n
is the length of the array, as we iterate through the array once to calculate both sums.
Space Complexity: O(1)
as we only use two variables to store the sums, regardless of input size.
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through a concrete example to see how this solution works.
Example: nums = [5, 17, 3, 20, 8, 12]
Step 1: Identify and sum single-digit numbers
- Single-digit numbers (< 10):
[5, 3, 8]
- Sum of single-digit numbers:
a = 5 + 3 + 8 = 16
Step 2: Identify and sum double-digit numbers
- Double-digit numbers (≥ 10):
[17, 20, 12]
- Sum of double-digit numbers:
b = 17 + 20 + 12 = 49
Step 3: Check if Alice can win
- Compare the sums:
a = 16
andb = 49
- Since
16 ≠ 49
, Alice can win! - Alice's winning strategy: Choose the double-digit numbers to get sum = 49
- Bob gets the single-digit numbers with sum = 16
- Alice wins because 49 > 16
What if the sums were equal?
Consider nums = [5, 2, 3, 10]
:
- Single-digit sum:
5 + 2 + 3 = 10
- Double-digit sum:
10
- Since both sums equal 10, no matter what Alice chooses, she gets 10 and Bob gets 10
- It's a tie, not a win, so the function returns
false
The beauty of this solution is that we don't need to explicitly check both scenarios. We just need to verify that the sums are different, because Alice will intelligently pick whichever group gives her the advantage.
Solution Implementation
1class Solution:
2 def canAliceWin(self, nums: List[int]) -> bool:
3 # Calculate sum of all single-digit numbers (0-9)
4 single_digit_sum = sum(num for num in nums if num < 10)
5
6 # Calculate sum of all double-digit numbers (10 and above)
7 double_digit_sum = sum(num for num in nums if num >= 10)
8
9 # Alice wins if the sums are different
10 # This means Alice can choose the larger sum and Bob gets the smaller one
11 return single_digit_sum != double_digit_sum
12
1class Solution {
2 /**
3 * Determines if Alice can win the game by choosing either single-digit or double-digit numbers.
4 * Alice wins if the sum of her chosen group is strictly greater than the sum of the remaining group.
5 *
6 * @param nums array of integers to be divided between Alice and Bob
7 * @return true if Alice can win by choosing either group, false otherwise
8 */
9 public boolean canAliceWin(int[] nums) {
10 // Sum of all single-digit numbers (0-9)
11 int singleDigitSum = 0;
12 // Sum of all double-digit numbers (10-99)
13 int doubleDigitSum = 0;
14
15 // Iterate through the array and categorize numbers by digit count
16 for (int number : nums) {
17 if (number < 10) {
18 // Add to single-digit sum
19 singleDigitSum += number;
20 } else {
21 // Add to double-digit sum (assumes all non-single-digit numbers are double-digit)
22 doubleDigitSum += number;
23 }
24 }
25
26 // Alice can win if the sums are different (she can choose the larger group)
27 // If sums are equal, neither choice gives Alice a strict advantage
28 return singleDigitSum != doubleDigitSum;
29 }
30}
31
1class Solution {
2public:
3 bool canAliceWin(vector<int>& nums) {
4 // Sum of single-digit numbers (less than 10)
5 int singleDigitSum = 0;
6
7 // Sum of double-digit numbers (10 and above)
8 int doubleDigitSum = 0;
9
10 // Iterate through all numbers in the array
11 for (int num : nums) {
12 // Check if the number is single-digit
13 if (num < 10) {
14 singleDigitSum += num;
15 } else {
16 // Number is double-digit (10 or greater)
17 doubleDigitSum += num;
18 }
19 }
20
21 // Alice wins if the sums are different
22 // (She can choose the larger sum category)
23 return singleDigitSum != doubleDigitSum;
24 }
25};
26
1/**
2 * Determines if Alice can win the game based on the sum of single-digit and multi-digit numbers
3 * @param nums - Array of numbers to be evaluated
4 * @returns true if Alice can win (sums are different), false otherwise
5 */
6function canAliceWin(nums: number[]): boolean {
7 // Sum of single-digit numbers (less than 10)
8 let singleDigitSum: number = 0;
9 // Sum of multi-digit numbers (10 or greater)
10 let multiDigitSum: number = 0;
11
12 // Iterate through all numbers and categorize them
13 for (const num of nums) {
14 if (num < 10) {
15 // Add to single-digit sum
16 singleDigitSum += num;
17 } else {
18 // Add to multi-digit sum
19 multiDigitSum += num;
20 }
21 }
22
23 // Alice wins if the two sums are different
24 return singleDigitSum !== multiDigitSum;
25}
26
Time and Space Complexity
The time complexity is O(n)
, where n
is the length of the array nums
. This is because the code iterates through the entire array twice - once to calculate the sum of single-digit numbers (x < 10
) and once to calculate the sum of double-digit numbers (x > 9
). Even though there are two iterations, they are sequential and not nested, resulting in O(n) + O(n) = O(2n) = O(n)
time complexity.
The space complexity is O(1)
. The code only uses two variables a
and b
to store the sums, which require constant space regardless of the input size. The generator expressions (x for x in nums if x < 10)
and (x for x in nums if x > 9)
are evaluated lazily and don't create intermediate lists, so they don't consume additional space proportional to the input size.
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Misunderstanding the Problem Constraints
Pitfall: Assuming the array only contains numbers from 1-99, when the problem states "positive integers" which could include numbers ≥ 100.
Issue Example:
# Incorrect assumption
def canAliceWin(self, nums: List[int]) -> bool:
single_sum = sum(x for x in nums if x < 10)
double_sum = sum(x for x in nums if x >= 10 and x <= 99) # Wrong!
return single_sum != double_sum
Solution: The problem states Alice chooses either single-digit (1-9) OR double-digit (10-99) numbers. Any number ≥ 100 automatically goes to Bob regardless of Alice's choice. The correct approach treats all numbers ≥ 10 as part of the "double-digit" group for calculation purposes:
def canAliceWin(self, nums: List[int]) -> bool:
single_sum = sum(x for x in nums if x < 10)
# Include ALL numbers >= 10, not just 10-99
double_sum = sum(x for x in nums if x >= 10)
return single_sum != double_sum
2. Off-by-One Error in Boundary Conditions
Pitfall: Using incorrect comparison operators for single vs. double-digit classification.
Issue Example:
# Incorrect boundaries
def canAliceWin(self, nums: List[int]) -> bool:
single_sum = sum(x for x in nums if x <= 9) # Includes 0 if present
double_sum = sum(x for x in nums if x > 10) # Excludes 10!
return single_sum != double_sum
Solution: Use precise boundaries:
- Single-digit:
1 <= x <= 9
or simplyx < 10
(assuming positive integers) - Double-digit and above:
x >= 10
3. Incorrect Win Condition Logic
Pitfall: Returning the wrong boolean condition or misunderstanding when Alice wins.
Issue Example:
# Wrong logic
def canAliceWin(self, nums: List[int]) -> bool:
single_sum = sum(x for x in nums if x < 10)
double_sum = sum(x for x in nums if x >= 10)
return single_sum > double_sum # Wrong! Alice can choose either group
Solution: Alice wins if she CAN make a choice that gives her a higher sum. This happens when the sums are different (she'll choose the larger one):
return single_sum != double_sum # Correct
You are given an array of intervals where intervals[i] = [start_i, end_i]
represent the start and end of the ith
interval. You need to merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.
Recommended Readings
Math for Technical Interviews How much math do I need to know for technical interviews The short answer is about high school level math Computer science is often associated with math and some universities even place their computer science department under the math faculty However the reality is that you
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask
Want a Structured Path to Master System Design Too? Don’t Miss This!