An adventurous ant is exploring a one-dimensional world where it starts at position 0 (the boundary). The ant follows a sequence of movement instructions, where each instruction tells it to move left or right by a certain number of units.
Given an array nums of non-zero integers representing movement instructions:
- If
nums[i] > 0, the ant moves right bynums[i]units - If
nums[i] < 0, the ant moves left by|nums[i]|units
Goal: Count how many times the ant returns to the boundary (position 0) after completing each movement instruction.
Note: We only check if the ant is at the boundary after each complete movement. If the ant crosses the boundary during a movement, it doesn't count as a return.
Input & Output
Visualization
Time & Space Complexity
Single pass through the array, processing each element once
Only using two variables: position and count
Constraints
- 1 โค nums.length โค 104
- -50 โค nums[i] โค 50
- nums[i] โ 0 (no zero movements)