Ant on the Boundary

Easy
563.

Ant on the Boundary

Ant on the Boundary is an easy coding problem on arrays, prefix sum and simulation, asked in Amazon and TCS interviews.

An ant starts on a boundary and reads nums left to right. A negative value moves it that many units left; a positive value moves it that many units right.

The ant only checks whether it is back on the boundary after fully completing each move. Return how many times it lands on the boundary.

Example 1

Input
nums = [2,3,-5]
Output
1
Explanation
Positions after each move: 2, 5, 0 — the boundary is reached once.

Example 2

Input
nums = [3,2,-3,-4]
Output
0
Explanation
Positions 3, 5, 2, -2 — never zero.

Example 3

Input
nums = [1,-1,1,-1]
Output
2

Constraints

  • 1 <= nums.length <= 100
  • -10 <= nums[i] <= 10
  • nums[i] is never 0.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var returnToBoundary = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Ant on the Boundary — Solution in Python, Java & C++

Ant on the Boundary solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Ant on the Boundary is an easy coding problem on arrays, prefix sum and simulation, asked in Amazon and TCS interviews.

  • Difficulty: Easy
  • Topics: Arrays, Prefix Sum, Simulation
  • Asked at: Amazon, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

An ant starts on a boundary and reads nums left to right. A negative value moves it that many units left; a positive value moves it that many units right.

The ant only checks whether it is back on the boundary after fully completing each move. Return how many times it lands on the boundary.

Example 1

Input: nums = [2,3,-5]
Output: 1
Explanation: Positions after each move: 2, 5, 0 — the boundary is reached once.

Example 2

Input: nums = [3,2,-3,-4]
Output: 0
Explanation: Positions 3, 5, 2, -2 — never zero.

Example 3

Input: nums = [1,-1,1,-1]
Output: 2

Constraints

  • 1 <= nums.length <= 100
  • -10 <= nums[i] <= 10
  • nums[i] is never 0.

Hints for Ant on the Boundary

  1. Hint 1

    Track a running position — that is just the prefix sum.

  2. Hint 2

    Check for zero only after the whole move has been applied.

  3. Hint 3

    Crossing the boundary mid-move does not count.

How to solve Ant on the Boundary

The ant's position after k moves is the prefix sum of the first k values, so the answer counts the zero prefix sums.

Approach

  1. Keep a running total starting at 0.
  2. Add each element in turn.
  3. Increment the counter whenever the running total equals 0 after an addition.

Why it works

The problem explicitly says the ant checks only after completing a move, which is exactly the prefix-sum-after-each-element reading.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Counting a zero starting position inflates the answer — the check happens only after a move.
  • Trying to detect boundary crossings during a move counts positions the ant never checks.

Reference solution

Python

from typing import List

def returnToBoundary(nums: List[int]) -> int:
    pos = 0
    count = 0
    for x in nums:
        pos += x
        if pos == 0:
            count += 1
    return count

Java

public static int returnToBoundary(int[] nums) {
    int pos = 0, count = 0;
    for (int x : nums) {
        pos += x;
        if (pos == 0) count++;
    }
    return count;
}

C++

int returnToBoundary(vector<int>& nums) {
    int pos = 0, count = 0;
    for (int x : nums) {
        pos += x;
        if (pos == 0) count++;
    }
    return count;
}

JavaScript

var returnToBoundary = function(nums) {
    let pos = 0, count = 0;
    for (let i = 0; i < nums.length; i++) {
        pos += nums[i];
        if (pos === 0) count++;
    }
    return count;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Prefix Sum