You are given an integer array arr. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...) jumps in the series are called odd-numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even-numbered jumps. Note that the jumps are numbered, not the indices.

You may jump forward from index i to index j (with i < j) in the following way:

  • During odd-numbered jumps (i.e., jumps 1, 3, 5, ...), you jump to the index j such that arr[i] <= arr[j] and arr[j] is the smallest possible value. If there are multiple such indices j, you can only jump to the smallest such index j.
  • During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index j such that arr[i] >= arr[j] and arr[j] is the largest possible value. If there are multiple such indices j, you can only jump to the smallest such index j.

It may be the case that for some index i, there are no legal jumps.

A starting index is good if, starting from that index, you can reach the end of the array (index arr.length - 1) by jumping some number of times (possibly 0 or more than once). Return the number of good starting indices.

Input & Output

Example 1 — Basic Jump Sequence
$ Input: arr = [10,13,12,14,15]
Output: 2
💡 Note: From index 0: 10→13(odd)→12(even)→14(odd)→15 reaches end. From index 2: 12→14(odd)→15 reaches end. Positions 1,3,4 cannot reach end due to jump constraints.
Example 2 — Single Element
$ Input: arr = [2]
Output: 1
💡 Note: Single element array - position 0 is already at the end, so it's a good starting index.
Example 3 — No Valid Jumps
$ Input: arr = [5,1,3,4,2]
Output: 3
💡 Note: From index 0: 5→no valid odd jump (need ≥5). From index 2: 3→4(odd) reaches end. From index 3: 4→no valid jump. Only indices 2, 3, 4 are good.

Constraints

  • 1 ≤ arr.length ≤ 2 × 104
  • 0 ≤ arr[i] < 105

Visualization

Tap to expand
Odd Even Jump - Dynamic Programming INPUT Array arr: 10 i=0 13 i=1 12 i=2 14 i=3 15 i=4 (Goal: reach index 4) Jump Rules: Odd Jump (1st, 3rd...): arr[i] <= arr[j], smallest arr[j] Even Jump (2nd, 4th...): arr[i] >= arr[j], largest arr[j] Example from i=3: 14 odd 15 14 <= 15, reaches end - OK ALGORITHM STEPS 1 Build Next Jump Maps Use sorted order + monotonic stack to find odd/even jump targets 2 Initialize DP Arrays odd[i] = can reach end via odd jump even[i] = can reach end via even 3 Fill DP (right to left) odd[i] = even[nextOdd[i]] even[i] = odd[nextEven[i]] DP Table (right to left): idx: 4 3 2 1 0 odd: T T F F F even: T F T T T Start with odd jump, count odd[i]=T 4 Count Good Indices Sum all odd[i] = True values (first jump is always odd) FINAL RESULT Good Starting Indices: 10 NO 13 NO 12 NO 14 OK 15 OK Path Analysis: i=3 (14): 14 --odd--> 15 (end) OK i=4 (15): Already at end OK i=0,1,2: Cannot reach end Output: 2 2 good starting indices (indices 3 and 4) Key Insight: Use a monotonic stack with sorted indices to efficiently find next jump targets in O(n log n). Process from right to left: odd[i] depends on even[target], and even[i] depends on odd[target]. The first jump is always odd, so count indices where odd[i] = True for the final answer. TutorialsPoint - Odd Even Jump | Dynamic Programming Approach
Asked in
Google 42 Facebook 23 Amazon 18
78.0K Views
Medium Frequency
~35 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen