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.
An ant is on a boundary. It sometimes goes left and sometimes right.
You are given an array of non-zero integers nums. The ant starts reading nums from the first element of it to its end. At each step, it moves according to the value of the current element:
nums[i] < 0, it moves left by -nums[i] units.nums[i] > 0, it moves right by nums[i] units.Return the number of times the ant returns to the boundary.
Notes:
|nums[i]| units. In other words, if the ant crosses the boundary during its movement, it does not count.Example 1:
Input: nums = [2,3,-5] Output: 1 Explanation: After the first step, the ant is 2 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is on the boundary. So the answer is 1.
Example 2:
Input: nums = [3,2,-3,-4] Output: 0 Explanation: After the first step, the ant is 3 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is 2 steps to the right of the boundary. After the fourth step, the ant is 2 steps to the left of the boundary. The ant never returned to the boundary, so the answer is 0.
Constraints:
1 <= nums.length <= 100-10 <= nums[i] <= 10nums[i] != 0Problem summary: An ant is on a boundary. It sometimes goes left and sometimes right. You are given an array of non-zero integers nums. The ant starts reading nums from the first element of it to its end. At each step, it moves according to the value of the current element: If nums[i] < 0, it moves left by -nums[i] units. If nums[i] > 0, it moves right by nums[i] units. Return the number of times the ant returns to the boundary. Notes: There is an infinite space on both sides of the boundary. We check whether the ant is on the boundary only after it has moved |nums[i]| units. In other words, if the ant crosses the boundary during its movement, it does not count.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,3,-5]
[3,2,-3,-4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3028: Ant on the Boundary
class Solution {
public int returnToBoundaryCount(int[] nums) {
int ans = 0, s = 0;
for (int x : nums) {
s += x;
if (s == 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3028: Ant on the Boundary
func returnToBoundaryCount(nums []int) (ans int) {
s := 0
for _, x := range nums {
s += x
if s == 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #3028: Ant on the Boundary
class Solution:
def returnToBoundaryCount(self, nums: List[int]) -> int:
return sum(s == 0 for s in accumulate(nums))
// Accepted solution for LeetCode #3028: Ant on the Boundary
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3028: Ant on the Boundary
// class Solution {
// public int returnToBoundaryCount(int[] nums) {
// int ans = 0, s = 0;
// for (int x : nums) {
// s += x;
// if (s == 0) {
// ++ans;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3028: Ant on the Boundary
function returnToBoundaryCount(nums: number[]): number {
let [ans, s] = [0, 0];
for (const x of nums) {
s += x;
ans += s === 0 ? 1 : 0;
}
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.