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 integer array nums.
Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right.
After that, you repeat the following process:
curr is out of the range [0, n - 1], this process ends.nums[curr] == 0, move in the current direction by incrementing curr if you are moving right, or decrementing curr if you are moving left.nums[curr] > 0:
nums[curr] by 1.A selection of the initial position curr and movement direction is considered valid if every element in nums becomes 0 by the end of the process.
Return the number of possible valid selections.
Example 1:
Input: nums = [1,0,2,0,3]
Output: 2
Explanation:
The only possible valid selections are the following:
curr = 3, and a movement direction to the left.
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].curr = 3, and a movement direction to the right.
[1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].Example 2:
Input: nums = [2,3,4,0,4,1,0]
Output: 0
Explanation:
There are no possible valid selections.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100i where nums[i] == 0.Problem summary: You are given an integer array nums. Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right. After that, you repeat the following process: If curr is out of the range [0, n - 1], this process ends. If nums[curr] == 0, move in the current direction by incrementing curr if you are moving right, or decrementing curr if you are moving left. Else if nums[curr] > 0: Decrement nums[curr] by 1. Reverse your movement direction (left becomes right and vice versa). Take a step in your new direction. A selection of the initial position curr and movement direction is considered valid if every element in nums becomes 0 by the end of the process. Return the number of possible valid selections.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,0,2,0,3]
[2,3,4,0,4,1,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3354: Make Array Elements Equal to Zero
class Solution {
public int countValidSelections(int[] nums) {
int s = Arrays.stream(nums).sum();
int ans = 0, l = 0;
for (int x : nums) {
if (x != 0) {
l += x;
} else if (l * 2 == s) {
ans += 2;
} else if (Math.abs(l * 2 - s) <= 1) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3354: Make Array Elements Equal to Zero
func countValidSelections(nums []int) (ans int) {
l, s := 0, 0
for _, x := range nums {
s += x
}
for _, x := range nums {
if x != 0 {
l += x
} else if l*2 == s {
ans += 2
} else if abs(l*2-s) <= 1 {
ans++
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #3354: Make Array Elements Equal to Zero
class Solution:
def countValidSelections(self, nums: List[int]) -> int:
s = sum(nums)
ans = l = 0
for x in nums:
if x:
l += x
elif l * 2 == s:
ans += 2
elif abs(l * 2 - s) == 1:
ans += 1
return ans
// Accepted solution for LeetCode #3354: Make Array Elements Equal to Zero
impl Solution {
pub fn count_valid_selections(nums: Vec<i32>) -> i32 {
let s: i32 = nums.iter().sum();
let mut ans = 0;
let mut l = 0;
for &x in &nums {
if x != 0 {
l += x;
} else if l * 2 == s {
ans += 2;
} else if (l * 2 - s).abs() <= 1 {
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #3354: Make Array Elements Equal to Zero
function countValidSelections(nums: number[]): number {
const s = nums.reduce((acc, x) => acc + x, 0);
let [ans, l] = [0, 0];
for (const x of nums) {
if (x) {
l += x;
} else if (l * 2 === s) {
ans += 2;
} else if (Math.abs(l * 2 - s) <= 1) {
++ans;
}
}
return ans;
}
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.