Minimum jumps required to reach all array elements using largest element - GeeksforGeeks

14 Jan, 2022
Given an array arr[] of N distinct integer, the task is to find the minimum number of jumps required from the largest element to reach all array elements such that a jump is possible from the ith element to the jth element if the value of arr[i] is greater than arr[j] and value of arr[j] is greater than all other elements between the ith and jth element.
Examples:
Input: arr[] = {1, 3, 6, 5}Output: [2, 1, 0, 1]Explanation:Below are the jumps required to reach each platform:
For the 1st platform, the jump from the 3rd platform to the 2nd platform, then jump to the 1st platform is required. Hence, a total of 2 jumps are required.
For the 2nd platform, the jump from the 3rd platform directly to the 2nd platform is required. Hence, a total of 1 jump are required.
For the 3rd platform, we are already on the 3rd platform. Hence, a total of 0 jumps are required.
For the 4th platform, the jump from the 3rd platform directly to the 4th platform is required. Hence, a total of 1 jump are required.
Input: arr[] = {3, 5}Output: [1, 0]
Approach: The given problem can be solved using Dynamic Programming which is based on the observation that the minimum jump possible from the largest element to the ith element is one greater than the minimum of minimum jumps required for the next greater element in the left or right. So, the idea is to precompute the results of larger elements and use them to find answers to smaller elements. Follow the steps below to solve the given problem:
For each array element arr[i] store the two indices L and R representing the index of the next greater element to the left and right in the map respectively.
Sort the array arr[] in descending order.
Initialize a vector, say ans[] that stores the minimum jumps for all array elements.
Traverse the array arr[] and perform the following steps:If the current array element is the largest element then 0 jumps are required for the current element.Find the distance of the next greater element to the left and right of the current element using the value stored in the maps. Store the distances in the variables, L and R respectively.Update the value of minimum jumps, say M as per the following criteria:If L is at least 0 and R is less than N, then the value of M is min(ans[L], ans[R]) + 1.If L is less than 0 and R is less than N, then the value of M is ans[R] + 1.If L is at least 0 and R is at least N, then the value of M is ans[L] + 1.Update the value of minimum jumps for the current index as the value of M.
If the current array element is the largest element then 0 jumps are required for the current element.
Find the distance of the next greater element to the left and right of the current element using the value stored in the maps. Store the distances in the variables, L and R respectively.
Update the value of minimum jumps, say M as per the following criteria:If L is at least 0 and R is less than N, then the value of M is min(ans[L], ans[R]) + 1.If L is less than 0 and R is less than N, then the value of M is ans[R] + 1.If L is at least 0 and R is at least N, then the value of M is ans[L] + 1.
If L is at least 0 and R is less than N, then the value of M is min(ans[L], ans[R]) + 1.
If L is less than 0 and R is less than N, then the value of M is ans[R] + 1.
If L is at least 0 and R is at least N, then the value of M is ans[L] + 1.
Update the value of minimum jumps for the current index as the value of M.
After completing the above steps, print the array ans[] as the resultant jumps of indices.
Below is the implementation of the above approach:
C++
Python3
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;#define ar array // Function to find next greater element// to left and right of current elementar<int, 2> expand(int idx, vector<int>& A){     // Starting l and r from previous    // and the next element of the    // current element    int l = idx - 1;    int r = idx + 1;     // FInd the next greater element    // to the left    while (l >= 0) {         if ((int)(A[idx]) > A[l]) {            --l;        }        else {            break;        }    }     if (l < 0 || l == idx) {        l = -2;    }     // Find the next greater element    // to the right    while (r < (int)(A.size())) {        if ((int)A[idx] > A[r]) {            ++r;        }        else {            break;        }    }     if (r >= (int)(A.size()) || r == idx) {        r = -2;    }     // Return l and r in the form of    // array of size 2    return { l, r };} // Function to find the minimum jumps// required to reach to all elements from// the largest elementvector<int> minJumps(int N, vector<int>& A){    vector<int> ans(N, 0);     // Stores the mapping from index    // to the element in array A[]    map<int, ar<int, 2> > mp;     map<int, int> iToA;    map<int, int> AToi;     // Stores largest array element    int big = A[0];     // Find the two indices l, r such    // that A[l] > A[i] < A[r] and    // l<i<r using expand function    for (int i = 0; i < N; ++i) {        big = max({ big, A[i] });        mp[i] = expand(i, A);         iToA[i] = A[i];        AToi[A[i]] = i;    }     // sorting A in descending order    sort(A.begin(), A.end(), greater<int>());     for (int i = 0; i < A.size(); ++i) {         // Stores the resultant minimum        // jumps required        int m;         // Check if the current element        // is largest or not        if (A[i] == big) {            int cur = AToi[A[i]];            ans[cur] = 0;            continue;        }         // Find the answer to the        // current element        int cur = AToi[A[i]];        int l = mp[cur][0];        int r = mp[cur][1];         if (l >= 0 && r < N) {            m = min(ans[l], ans[r]) + 1;        }        else if (l < 0 && r < N) {            m = ans[r] + 1;        }        else if (l >= 0 && r >= N) {            m = ans[l] + 1;        }         // Update the resultant minimum        // jumps for the current element        ans[cur] = m;    }     // Return the result    return ans;} // Driver Codeint main(){    vector<int> arr = { 5, 1, 3, 4, 7 };    int N = arr.size();     vector<int> out = minJumps(N, arr);     // Print the result    for (auto& it : out)        cout << it << ' ';     return 0;}
# Python program for the above approach # Function to find next greater element# to left and right of current elementdef expand(idx, A):     # Starting l and r from previous    # and the next element of the    # current element    l = idx - 1    r = idx + 1     # FInd the next greater element    # to the left    while (l >= 0):        if (A[idx] > A[l]):          l -= 1        else:          break     if (l < 0 or l == idx):        l = -2     # Find the next greater element    # to the right    while (r < len(A)):        if (A[idx] > A[r]):            r += 1        else:            break     if (r >= len(A) or r == idx):        r = -2     # Return l and r in the form of    # array of size 2    return [l, r]  # Function to find the minimum jumps# required to reach to all elements from# the largest elementdef minJumps(N, A):    ans = [0 for i in range(N)]     # Stores the mapping from index    # to the element in array A[]    mp = {}     iToA = {}    AToi = {}     # Stores largest array element    big = A[0]     # Find the two indices l, r such    # that A[l] > A[i] < A[r] and    # l<i<r using expand function    for i in range(N):        big = max(big, A[i])        mp[i] = expand(i, A)         iToA[i] = A[i]        AToi[A[i]] = i     # sorting A in descending order    A = sorted(A, reverse=True)     for i in range(len(A)):        # Stores the resultant minimum        # jumps required        m = None         # Check if the current element        # is largest or not        if (A[i] == big):            cur = AToi[A[i]]            ans[cur] = 0            continue         # Find the answer to the        # current element        cur = AToi[A[i]]        l = mp[cur][0]        r = mp[cur][1]         if (l >= 0 and r < N):            m = min(ans[l], ans[r]) + 1        elif (l < 0 and r < N):            m = ans[r] + 1        elif (l >= 0 and r >= N):            m = ans[l] + 1         # Update the resultant minimum        # jumps for the current element        ans[cur] = m     # Return the result    return ans # Driver Codearr = [5, 1, 3, 4, 7]N = len(arr) out = minJumps(N, arr) # Print the resultfor it in out:    print(it, end=" ")     # This code is contributed by saurabh_jaiswal.
<script>// Javascript program for the above approach // Function to find next greater element// to left and right of current elementfunction expand(idx, A){   // Starting l and r from previous  // and the next element of the  // current element  let l = idx - 1;  let r = idx + 1;   // FInd the next greater element  // to the left  while (l >= 0) {    if (A[idx] > A[l]) {      --l;    } else {      break;    }  }   if (l < 0 || l == idx) {    l = -2;  }   // Find the next greater element  // to the right  while (r < A.length) {    if (A[idx] > A[r]) {      ++r;    } else {      break;    }  }   if (r >= A.length || r == idx) {    r = -2;  }   // Return l and r in the form of  // array of size 2  return [l, r];} // Function to find the minimum jumps// required to reach to all elements from// the largest elementfunction minJumps(N, A) {  let ans = new Array(N).fill(0);   // Stores the mapping from index  // to the element in array A[]  let mp = new Map();   let iToA = new Map();  let AToi = new Map();   // Stores largest array element  let big = A[0];   // Find the two indices l, r such  // that A[l] > A[i] < A[r] and  // l<i<r using expand function  for (let i = 0; i < N; ++i) {    big = Math.max(big, A[i]);    mp.set(i, expand(i, A));     iToA.set(i, A[i]);    AToi.set(A[i], i);  }   // sorting A in descending order  A.sort((a, b) => - a + b);   for (let i = 0; i < A.length; ++i) {    // Stores the resultant minimum    // jumps required    let m;     // Check if the current element    // is largest or not    if (A[i] == big) {      let cur = AToi.get(A[i]);      ans[cur] = 0;      continue;    }     // Find the answer to the    // current element    let cur = AToi.get(A[i]);    let l = mp.get(cur)[0];    let r = mp.get(cur)[1];     if (l >= 0 && r < N) {      m = Math.min(ans[l], ans[r]) + 1;    } else if (l < 0 && r < N) {      m = ans[r] + 1;    } else if (l >= 0 && r >= N) {      m = ans[l] + 1;    }     // Update the resultant minimum    // jumps for the current element    ans[cur] = m;  }   // Return the result  return ans;} // Driver Code let arr = [5, 1, 3, 4, 7];let N = arr.length; let out = minJumps(N, arr); // Print the resultfor (it of out) document.write(it + " "); // This code is contributed by gfgking.</script>
  
1 2 2 1 0
 
 
Time Complexity: O(N2)Auxiliary Space: O(N)
 
gfgking
duttaaniruddha31
_saurabh_jaiswal
khushboogoyal499
Arrays
Dynamic Programming
Greedy
Mathematical
Arrays
Dynamic Programming
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Linear Search
Maximum and minimum of an array using minimum number of comparisons
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Bellman–Ford Algorithm | DP-23
Longest Common Subsequence | DP-4
Floyd Warshall Algorithm | DP-16