Odd Even Jump
HardPrompt
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
jsuch thatarr[i] <= arr[j]andarr[j]is the smallest possible value. If there are multiple such indicesj, you can only jump to the smallest such indexj. - During even-numbered jumps (i.e., jumps 2, 4, 6, ...), you jump to the index
jsuch thatarr[i] >= arr[j]andarr[j]is the largest possible value. If there are multiple such indicesj, you can only jump to the smallest such indexj. - 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.
Example 1:
Input: arr = [10,13,12,14,15]
Output: 2
Explanation:
From starting index i = 0, we can make our 1st jump to i = 2 (since arr[2] is the smallest among arr[1], arr[2], arr[3], arr[4] that is greater or equal to arr[0]), then we cannot jump any more.
From starting index i = 1 and i = 2, we can make our 1st jump to i = 3, then we cannot jump any more.
From starting index i = 3, we can make our 1st jump to i = 4, so we have reached the end.
From starting index i = 4, we have reached the end already.
In total, there are 2 different starting indices i = 3 and i = 4, where we can reach the end with some number of
jumps.Example 2:
Input: arr = [2,3,1,1,4]
Output: 3
Explanation:
From starting index i = 0, we make jumps to i = 1, i = 2, i = 3:
During our 1st jump (odd-numbered), we first jump to i = 1 because arr[1] is the smallest value in [arr[1], arr[2], arr[3], arr[4]] that is greater than or equal to arr[0].
During our 2nd jump (even-numbered), we jump from i = 1 to i = 2 because arr[2] is the largest value in [arr[2], arr[3], arr[4]] that is less than or equal to arr[1]. arr[3] is also the largest value, but 2 is a smaller index, so we can only jump to i = 2 and not i = 3
During our 3rd jump (odd-numbered), we jump from i = 2 to i = 3 because arr[3] is the smallest value in [arr[3], arr[4]] that is greater than or equal to arr[2].
We can't jump from i = 3 to i = 4, so the starting index i = 0 is not good.
In a similar manner, we can deduce that:
From starting index i = 1, we jump to i = 4, so we reach the end.
From starting index i = 2, we jump to i = 3, and then we can't jump anymore.
From starting index i = 3, we jump to i = 4, so we reach the end.
From starting index i = 4, we are already at the end.
In total, there are 3 different starting indices i = 1, i = 3, and i = 4, where we can reach the end with some
number of jumps.Example 3:
Input: arr = [5,1,3,4,2]
Output: 3
Explanation: We can reach the end from starting indices 1, 2, and 4.
Constraints:
1 <= arr.length <= 2 * 1040 <= arr[i] < 105
Approaches
2 approaches with complexity analysis and trade-offs.
This approach uses dynamic programming by working backward from the end of the array. For each index i, we determine if it's possible to reach the end starting with an odd jump (odd[i]) or an even jump (even[i]). To find the next jump from i, we perform a linear scan through the rest of the array, which is a brute-force method.
Algorithm
- Initialize
nas the length of the input arrayarr. - Create two boolean arrays,
oddandeven, of sizento store the dynamic programming states. - The base case is the last index. Since it's the target, we can reach it from itself. So, set
odd[n-1] = trueandeven[n-1] = true. - Iterate backwards from
i = n - 2down to0. - For each index
i:- To find the next odd jump target, iterate from
j = i + 1ton - 1. Keep track of the best candidate indexoddNextJumpthat corresponds to the minimumarr[j]such thatarr[j] >= arr[i]. The tie-breaking rule (smallest index) is naturally handled by not updating the target if a value equal to the current minimum is found at a larger index. - If a valid
oddNextJumpis found, setodd[i] = even[oddNextJump]. - Similarly, to find the next even jump target, iterate from
j = i + 1ton - 1. Find the best candidateevenNextJumpcorresponding to the maximumarr[j]such thatarr[j] <= arr[i]. - If a valid
evenNextJumpis found, seteven[i] = odd[evenNextJump].
- To find the next odd jump target, iterate from
- After filling the DP arrays, count the number of
truevalues in theoddarray. This is the result, as the first jump from any starting index is an odd-numbered jump.
Walkthrough
We define two boolean arrays, odd and even, of the same size as the input array arr. odd[i] is true if we can reach the last index starting from i with an odd-numbered jump. even[i] is true if we can reach the last index starting from i with an even-numbered jump.
The base case is the last index, n-1. Since we are already at the end, both odd[n-1] and even[n-1] are true.
We iterate from i = n-2 down to 0. For each i:
- To calculate
odd[i], we need to find the destinationjof the next (odd) jump. This involves searching all indicesk > ito find one wherearr[k] >= arr[i]andarr[k]is minimized. If multiple suchkexist, the smallestkis chosen. If such ajis found,odd[i]becomeseven[j], because the next jump fromjwill be an even jump. If no suchjexists,odd[i]is false. - Similarly, to calculate
even[i], we find the destinationkof the next (even) jump. This requires finding an indexl > iwherearr[l] <= arr[i]andarr[l]is maximized. If a tie occurs, the smallestlis chosen. If such akis found,even[i]becomesodd[k]. Otherwise,even[i]is false.
The search for the next jump target for each i takes O(n-i) time. Finally, the total number of good starting indices is the count of true values in the odd array, since the first jump is always an odd jump.
class Solution { public int oddEvenJumps(int[] arr) { int n = arr.length; if (n <= 1) { return n; } boolean[] odd = new boolean[n]; boolean[] even = new boolean[n]; odd[n - 1] = true; even[n - 1] = true; int goodIndices = 1; // The last index is always a good starting point for (int i = n - 2; i >= 0; i--) { // Find next odd jump int oddNextJump = -1; int minValOdd = Integer.MAX_VALUE; for (int j = i + 1; j < n; j++) { if (arr[j] >= arr[i]) { if (arr[j] < minValOdd) { minValOdd = arr[j]; oddNextJump = j; } } } // Find next even jump int evenNextJump = -1; int maxValEven = Integer.MIN_VALUE; for (int j = i + 1; j < n; j++) { if (arr[j] <= arr[i]) { if (arr[j] > maxValEven) { maxValEven = arr[j]; evenNextJump = j; } } } if (oddNextJump != -1) { odd[i] = even[oddNextJump]; } if (evenNextJump != -1) { even[i] = odd[evenNextJump]; } if (odd[i]) { goodIndices++; } } return goodIndices; }}Complexity
Time
O(N^2), where N is the number of elements in the array. The main loop runs N times, and inside it, we perform two linear scans of the subarray to the right, which takes up to O(N) time.
Space
O(N), where N is the number of elements in the array. This is for the `odd` and `even` DP arrays.
Trade-offs
Pros
The logic is straightforward and relatively easy to understand and implement.
Cons
The
O(N^2)time complexity makes it too slow for the given constraints, leading to a Time Limit Exceeded (TLE) error on larger test cases.
Solutions
Solution
class Solution {private int n;private Integer[][] f;private int[][] g;public int oddEvenJumps(int[] arr) { TreeMap<Integer, Integer> tm = new TreeMap<>(); n = arr.length; f = new Integer[n][2]; g = new int[n][2]; for (int i = n - 1; i >= 0; --i) { var hi = tm.ceilingEntry(arr[i]); g[i][1] = hi == null ? -1 : hi.getValue(); var lo = tm.floorEntry(arr[i]); g[i][0] = lo == null ? -1 : lo.getValue(); tm.put(arr[i], i); } int ans = 0; for (int i = 0; i < n; ++i) { ans += dfs(i, 1); } return ans; }private int dfs(int i, int k) { if (i == n - 1) { return 1; } if (g[i][k] == -1) { return 0; } if (f[i][k] != null) { return f[i][k]; } return f[i][k] = dfs(g[i][k], k ^ 1); }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.