Count Starts That Reach the End under Alternating Ordered Jumps

Quick Overview

Count Starts That Reach the End under Alternating Ordered Jumps. From index i, every jump goes to a larger index j.

Count Starts That Reach the End under Alternating Ordered Jumps

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem From index i, every jump goes to a larger index j. Odd-numbered jumps choose the smallest value arr[j] at least arr[i]; even-numbered jumps choose the largest value arr[j] at most arr[i]. When several indices have that chosen value, choose the smallest index. Jump numbering restarts at1 for each starting index. Return how many starts reach the last index, including the last index itself with zero jumps. ```cpp class Solution { public: int oddEvenJumps(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text Input: arr = [1,2,3] Output: 2 Explanation: Start0 goes to1 on an odd jump but has no legal even jump afterward. Starts1 and2 reach the end. ``` **Example 2:** ```text Input: arr = [5,5,5] Output: 3 Explanation: The value tie rule selects the next index, and equality permits both odd and even jumps. ``` ### Constraints - `1 <= arr.length <= 2*10^4`. - `0 <= arr[i] < 10^5`. ### Contract considerations Jump destinations are fixed by the source tie rules; this task does not allow choosing any convenient forward neighbor. ```hint Read the boundary condition Once forced destinations are known, can success be computed from right to left? ``` ### Boundary reminders Jump destinations are fixed by the source tie rules; this task does not allow choosing any convenient forward neighbor.

Overview: Count Starts That Reach the End under Alternating Ordered Jumps. From index i, every jump goes to a larger index j.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

From index i, every jump goes to a larger index j. Odd-numbered jumps choose the smallest value arr[j] at least arr[i]; even-numbered jumps choose the largest value arr[j] at most arr[i]. When several indices have that chosen value, choose the smallest index. Jump numbering restarts at1 for each starting index. Return how many starts reach the last index, including the last index itself with zero jumps.

class Solution {
public:
    int oddEvenJumps(vector<int>& arr);
};

Examples

Example 1:

Input: arr = [1,2,3]
Output: 2
Explanation: Start0 goes to1 on an odd jump but has no legal even jump afterward. Starts1 and2 reach the end.

Example 2:

Input: arr = [5,5,5]
Output: 3
Explanation: The value tie rule selects the next index, and equality permits both odd and even jumps.

Constraints

  • 1 <= arr.length <= 2*10^4 .
  • 0 <= arr[i] < 10^5 .

Contract considerations

Jump destinations are fixed by the source tie rules; this task does not allow choosing any convenient forward neighbor.

Boundary reminders

Jump destinations are fixed by the source tie rules; this task does not allow choosing any convenient forward neighbor.