Odd Even Jump in C++


Suppose we have an array A. From some starting index, we can make a series of jumps. The position (1, 3, 5, ...) jumps in the series are called odd numbered jumps, and position (2, 4, 6, ...) jumps in the series are called even numbered jumps.

Now we may from index i jump forward to index j (with i < j) in this way −

  • During odd numbered jumps, we can jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value. When there are multiple such indexes j, we can only jump to the smallest such index j.

  • During even numbered jumps, we can jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value. When there are multiple such indexes j, we can only jump to the smallest such index j.

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

Now a starting index is called good when, starting from that index, we can reach the end of the array by jumping some number of times.

We have to find the number of good starting indexes.

So, if the input is like [10,13,12,14,15], then the output will be 2, as there are two place index 3 and 4 from where we can reach at the end.

To solve this, we will follow these steps −

  • ret := 1

  • n := size of A

  • Define an array nextGreaterEqual of size n fill this with -1

  • Define an array nextSmallerEqual of size n fill this with -1

  • Define one map st

  • for initialize i := n - 1, when i >= 0, update (decrease i by 1), do −

    • if := The key-value pair whose value is not greater than A[i]

    • nextGreaterEqual[i] := if (it) is not the last element, then value of it, otherwise -1

    • if it is not equal to last element of st and first of it is same as A[i], then −

      • (increase it by 1)

    • nextSmallerEqual[i] := if (it) is not the first element, then value of previous of it, otherwise -1

    • st[A[i]] := i

  • Define one 2D array v of size n x 2 and fill this with false

  • v[n - 1, 0] = v[n - 1, 1] = true

  • for initialize i := n - 2, when i >= 0, update (decrease i by 1), do −

    • if nextGreaterEqual[i] is not equal to -1, then −

      • v[i, 1] := v[nextGreaterEqual[i], 0]

    • if nextSmallerEqual[i] is not equal to -1, then −

      • v[i, 0] := v[nextSmallerEqual[i], 1]

    • if v[i, 1] is non-zero, then −

      • (increase ret by 1)

  • return ret

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   int oddEvenJumps(vector<int>& A){
      int ret = 1;
      int n = A.size();
      vector<int> nextGreaterEqual(n, -1);
      vector<int> nextSmallerEqual(n, -1);
      map<int, int> st;
      for (int i = n - 1; i >= 0; i--) {
         map<int, int>::iterator it = st.lower_bound(A[i]);
         nextGreaterEqual[i] = (it != st.end()) ? it->second : -1;
         if (it != st.end() && it->first == A[i])
         it++;
         nextSmallerEqual[i] = it != st.begin() ? prev(it)->second
         : -1;
         st[A[i]] = i;
      }
      vector<vector<bool> > v(n, vector<bool>(2, false));
      v[n - 1][0] = v[n - 1][1] = true;
      for (int i = n - 2; i >= 0; i--) {
         if (nextGreaterEqual[i] != -1) {
            v[i][1] = v[nextGreaterEqual[i]][0];
         }
         if (nextSmallerEqual[i] != -1) {
            v[i][0] = v[nextSmallerEqual[i]][1];
         }
         if (v[i][1])
         ret++;
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<int> v = {10,13,12,14,15};
   cout << (ob.oddEvenJumps(v));
}

Input

{10,13,12,14,15}

Output

2

Updated on: 08-Jun-2020

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements