Problem Description
Given an array of positive integers, Alice and Bob play a game where Alice can choose either all single-digit numbers or all double-digit numbers. The rest of the numbers go to Bob. Alice wins if the sum of her chosen numbers is strictly greater than the sum of Bob's numbers. Return true if Alice can win the game by picking one of the two groups; otherwise, return false.
Key Insights
- Separate the numbers into two groups: single-digit numbers (1-9) and double-digit numbers (10-99).
- There are exactly two scenarios:
- Alice picks all single-digit numbers and Bob gets all double-digit numbers.
- Alice picks all double-digit numbers and Bob gets all single-digit numbers.
- Compute the sum for each group and compare according to the game rule.
Space and Time Complexity
Time Complexity: O(n) where n is the number of elements in the array. Space Complexity: O(1), using only a fixed number of extra variables.
Solution
The solution involves iterating through the array once to compute the sum of single-digit numbers and the sum of double-digit numbers. Then, evaluate the two possible scenarios:
- If the sum of single-digit numbers is strictly greater than the sum of double-digit numbers.
- If the sum of double-digit numbers is strictly greater than the sum of single-digit numbers. If either condition is met, Alice can win the game. This method uses simple arithmetic and constant extra space.