NewThe Learning module is now liveSee what's new

You are standing on a one-dimensional array nums at a starting position index. You also have an internal value called currentHeight, which is initialized to nums[index]. From there, you attempt to traverse the array by jumping between indices.

For each jump:

  • The first jump goes to the left. After every successful jump, the direction flips between left and right.
  • In the current direction, you must land on the nearest index i such that nums[i] == currentHeight + 1.
  • The current index itself is never a valid landing target.
  • After a successful jump, update currentHeight = currentHeight + x. This update is based on the previous currentHeight, not on nums[i] at the landing position.
  • If no valid landing index exists in the current direction, the traversal stops.

Return the final index where the traversal stops. This may be the starting index if the first jump is not possible.

Constraints:

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 104
  • 0 ≤ index < nums.length
  • 1 ≤ x ≤ 104

Example 1:

Input: nums = [3, 5, 1, 4, 2, 5, 6], index = 4, x = 1 Output: 6 Explanation: Starting at index 4 with currentHeight = 2 and direction = left, the traversal jumps along the sequence 4 → 0 → 3 → 1 → 6, with currentHeight growing 2 → 3 → 4 → 5 → 6. After landing at index 6, the next required target is 7, which no index holds, so the traversal stops at index 6.

Example 2:

Input: nums = [3, 5, 1, 4, 2, 6], index = 4, x = 2 Output: 1

Example 3:

Input: nums = [5], index = 0, x = 1 Output: 0


Accepted
170/510
Acceptance Rate33.3%
Test Cases
Input
nums =
index =
x =
Output
Expected