You are given a list of non-zero whole numbers, nums, laid out in a circle. Standing on index i, you jump nums[i] places: forward (to higher indices) when it is positive, backward when it is negative. Going past the last index wraps around to index 0, and going back from index 0 wraps to the last one.
Return true if some loop exists: a set of indices that, following the jumps, you visit over and over, where every jump in the loop goes the same direction and the loop has at least two indices. Return false otherwise. A number that jumps back onto its own index does not count.
Example 1
Input: nums = [-1, 3, 1, -2, 2] Output: true Why: 1 → 4 → 1 → ..., and both jumps go forward
2 more examples and the constraints
Example 2
Input: nums = [3, -2, 4] Output: false Why: 0 jumps onto itself; other paths change direction
Example 3
Input: nums = [1, -1, 1, -1] Output: false Why: 0 → 1 → 0 loops, but it goes forward then backward
Constraints
- 1 ≤ length of nums ≤ 5,000
- −1,000 ≤ nums[i] ≤ 1,000, and nums[i] ≠ 0
- Follow-up: O(n) time and O(1) extra space
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Walk from every startO(n²)
- Fast and slow pointers, clear dead pathsO(n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 5,000 numbers | Verdict |
|---|---|---|---|
| Walk from every start | O(n²) | Clear, but repeats walks | |
| Fast and slow pointers, clear dead paths | O(n) | Best; meets the follow-up |
n = how many numbers are in the list.
Edge cases to test
- One number. [5] always jumps onto itself, so false.
- Self-loop. In [3, -2, 4], index 0 jumps 3 places in a circle of 3 and lands on itself; it does not count.
- Mixed directions. [1, -1, 1, -1] has a loop 0 → 1 → 0, but it goes forward then backward: false.
- Jumps longer than the list. [1000, 1000, 1000] wraps many times; 0 → 1 → 2 → 0 is a loop: true.
- Negative wrap. In Java, C++ and TypeScript,
%can return a negative number; add n and take%again.
Common mistakes
- Forgetting that a self-loop of one index does not count.
- Using
(i + nums[i]) % nin Java, C++ or TypeScript without fixing negative results. - Checking only that the loop exists, not that every jump in it goes the same direction.
How to talk through it
- 1Clarify. Does a single index jumping onto itself count? Must the whole loop go one way? Can I change the input?
- 2Give the brute force. Walk n jumps from every start and watch for a direction flip or a self-loop: O(n²).
- 3See the linked list. Each index has one next index, so cycle detection with fast and slow pointers applies.
- 4Code it with clearing. Move the pointers while the direction holds; on failure, zero out the path so it is never walked again.
- 5Test out loud. Run a self-loop, a mixed-direction loop and Example 1 before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?