Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [1,2,3,4,10]
Output: false
Explanation:
Alice cannot win by choosing either single-digit or double-digit numbers.
Example 2:
Input: nums = [1,2,3,4,5,14]
Output: true
Explanation:
Alice can win by choosing single-digit numbers which have a sum equal to 15.
Example 3:
Input: nums = [5,5,5,25]
Output: true
Explanation:
Alice can win by choosing double-digit numbers which have a sum equal to 25.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 99Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3,4,10]
[1,2,3,4,5,14]
[5,5,5,25]
find-numbers-with-even-number-of-digits)count-integers-with-even-digit-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3232: Find if Digit Game Can Be Won
class Solution {
public boolean canAliceWin(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
}
// Accepted solution for LeetCode #3232: Find if Digit Game Can Be Won
func canAliceWin(nums []int) bool {
a, b := 0, 0
for _, x := range nums {
if x < 10 {
a += x
} else {
b += x
}
}
return a != b
}
# Accepted solution for LeetCode #3232: Find if Digit Game Can Be Won
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
// Accepted solution for LeetCode #3232: Find if Digit Game Can Be Won
/**
* [3232] Find if Digit Game Can Be Won
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn can_alice_win(nums: Vec<i32>) -> bool {
nums.into_iter()
.map(|x| if x >= 10 { (0, x) } else { (x, 0) })
.fold([(0, 0)], |acc, e| [(acc[0].0 + e.0, acc[0].1 + e.1)])
.iter()
.all(|x| x.0 != x.1)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3232() {
assert!(!Solution::can_alice_win(vec![1, 2, 3, 4, 10]));
assert!(Solution::can_alice_win(vec![1, 2, 3, 4, 5, 14]));
assert!(Solution::can_alice_win(vec![5, 5, 5, 25]));
}
}
// Accepted solution for LeetCode #3232: Find if Digit Game Can Be Won
function canAliceWin(nums: number[]): boolean {
let [a, b] = [0, 0];
for (const x of nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a !== b;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.