Tap here to switch tabs
Problem
Submissions

Problem: Odd Even Jump

med
30 min
Try to solve the Odd Even Jump problem.

Statement

Given an integer array arr, you start at some index i and repeatedly jump to a larger index until you can no longer jump or you reach the last index.

A jump count starts at 11 for the first jump, 22 for the second jump, and so on.
For a jump from index i, you may only jump to an index j where j>ij > i, and the destination j is chosen by these rules:

For an odd numbered jump (the 11st, 33rd, 55th, … jump):
Select the index j such that arr[j] is the smallest value among all arr[k] with k>ik > i and arr[k] >= arr[i].
If multiple indices have the same chosen value, pick the smallest index.

For an even numbered jump (the 22nd, 44th, 66th, … jump):
Select the index j such that arr[j] is the largest value among all arr[k] with k>ik > i and arr[k] <= arr[i].
If multiple indices have the same chosen value, pick the smallest index.

An index i is considered good if, starting from i and following these rules, you can eventually reach the last index of arr. Return the number of good starting indices.

Constraints:

  • 1≤1 \leq arr.length ≤2⋅104\leq 2 \cdot 10^4

  • 0≤0 \leq arr[i] <105< 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Odd Even Jump

med
30 min
Try to solve the Odd Even Jump problem.

Statement

Given an integer array arr, you start at some index i and repeatedly jump to a larger index until you can no longer jump or you reach the last index.

A jump count starts at 11 for the first jump, 22 for the second jump, and so on.
For a jump from index i, you may only jump to an index j where j>ij > i, and the destination j is chosen by these rules:

For an odd numbered jump (the 11st, 33rd, 55th, … jump):
Select the index j such that arr[j] is the smallest value among all arr[k] with k>ik > i and arr[k] >= arr[i].
If multiple indices have the same chosen value, pick the smallest index.

For an even numbered jump (the 22nd, 44th, 66th, … jump):
Select the index j such that arr[j] is the largest value among all arr[k] with k>ik > i and arr[k] <= arr[i].
If multiple indices have the same chosen value, pick the smallest index.

An index i is considered good if, starting from i and following these rules, you can eventually reach the last index of arr. Return the number of good starting indices.

Constraints:

  • 1≤1 \leq arr.length ≤2⋅104\leq 2 \cdot 10^4

  • 0≤0 \leq arr[i] <105< 10^5