Minimum number of jumps required to sort the given array in ascending order
Given two arrays arr[] and jump[], each of length N, where jump[i] denotes the number of indices by which the ith element in the array arr[] can move forward, the task is to find the minimum number of jumps required such that the array is sorted in ascending order.
Note:
All elements of the array arr[] are distinct.
While jumping, array elements can overlap (i.e. lie on the same index).
Array elements can move to the indices exceeding the size of the array.
Examples:
Input: arr[] = {3, 1, 2}, jump[] = {1, 4, 5}
Output: 3
Explanation:
Following sequence requires minimum number of jumps to sort the array in ascending order:
Jump 1: arr[0] jumps by 1 ( = jump[0]) index to index 1.
Jump 2: arr[0] jumps by 1 ( = jump[0]) index to index 2.
Jump 3: arr[0] jumps by 1 ( = jump[0]) index to index 3.
Therefore, the minimum number of operations required is 3.Input: arr[] = {3, 2, 1}, jump[] = {1, 1, 1}
Output: 6
Explanation:
Following sequence requires minimum number of jumps to sort the array in ascending order:
Jump 1: arr[0] jumps by 1 ( = jump[0]) index to the index 1.
Jump 2: arr[0] jumps by 1 ( = jump[0]) index to the index 2.
Jump 3: arr[0] jumps by 1 ( = jump[0]) index to the index 3.
Jump 4: arr[1] jumps by 1 ( = jump[0]) index to the index 2.
Jump 5: arr[1] jumps by 1 ( = jump[0]) index to the index 3.
Jump 6: arr[0] jumps by 1 ( = jump[0]) index to the index 4.
Therefore, the minimum number of operations required is 6.
Approach: The given problem can be solved greedily. Follow the steps below to solve the problem:
- Initialize a variable, say jumps = 0, to store the minimum number of jumps required.
- Initialize an array, say temp[], where temp[arr[i]] stores the distance that can be jumped by arr[i]
- Initialize a vector of pairs, say vect, to store the elements of the array arr[] and their respective indices i.e {arr[i], i + 1}
- Sort the vector vect.
- Traverse the vector vect, over the range of indices [1, N – 1]. Update the value of vect[i] while vect[i].second ≤ vect[i-1].second, by adding the distance of jump to vect[i].second.
- Increment jumps by 1.
- Print the value of jumps.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count minimum number // of jumps required to sort the array void minJumps( int arr[], int jump[], int N) { // Stores minimum number of jumps int jumps = 0; // Stores distances of jumps int temp[1000]; // Stores the array elements // with their starting indices vector<pair< int , int > > vect; // Push the pairs {arr[i], i+1} // into the vector of pairs vect for ( int i = 0; i < N; i++) { // Update vect vect.push_back({ arr[i], i + 1 }); } // Populate the array temp[] for ( int i = 0; i < N; i++) { // Update temp[arr[i]] temp[arr[i]] = jump[i]; } // Sort the vector in // the ascending order sort(vect.begin(), vect.end()); for ( int i = 1; i < N; i++) { // Jump till the previous // index <= current index while (vect[i].second <= vect[i - 1].second) { // Update vect[i] vect[i] = make_pair(vect[i].first, vect[i].second + temp[vect[i].first]); // Increment the // number of jumps jumps++; } } // Print the minimum number // of jumps required cout << jumps << endl; } // Driver Code int main() { // Input int arr[] = { 3, 2, 1 }; int jump[] = { 1, 1, 1 }; // Size of the array int N = sizeof (arr) / sizeof (arr[0]); minJumps(arr, jump, N); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG{ // Function to count minimum number // of jumps required to sort the array static void minJumps( int arr[], int jump[], int N) { // Stores minimum number of jumps int jumps = 0 ; // Stores distances of jumps int temp[] = new int [ 1000 ]; // Stores the array elements // with their starting indices int vect[][] = new int [N][ 2 ]; // Push the pairs {arr[i], i+1} // into the vector of pairs vect for ( int i = 0 ; i < N; i++) { // Update vect vect[i][ 0 ] = arr[i]; vect[i][ 1 ] = i + 1 ; } // Populate the array temp[] for ( int i = 0 ; i < N; i++) { // Update temp[arr[i]] temp[arr[i]] = jump[i]; } // Sort the vector in // the ascending order Arrays.sort(vect, (a, b) -> { if (a[ 0 ] != b[ 0 ]) return a[ 0 ] - b[ 0 ]; return a[ 1 ] - b[ 1 ]; }); for ( int i = 1 ; i < N; i++) { // Jump till the previous // index <= current index while (vect[i][ 1 ] <= vect[i - 1 ][ 1 ]) { // Update vect[i] vect[i][ 0 ] = vect[i][ 0 ]; vect[i][ 1 ] = vect[i][ 1 ] + temp[vect[i][ 0 ]]; // Increment the // number of jumps jumps++; } } // Print the minimum number // of jumps required System.out.println(jumps); } // Driver Code public static void main(String[] args) { // Input int arr[] = { 3 , 2 , 1 }; int jump[] = { 1 , 1 , 1 }; // Size of the array int N = arr.length; minJumps(arr, jump, N); } } // This code is contributed by Kingash |
Python3
# Python3 program for the above approach # Function to count minimum number # of jumps required to sort the array def minJumps(arr, jump, N): # Stores minimum number of jumps jumps = 0 # Stores distances of jumps temp = [ 0 for i in range ( 1000 )] # Stores the array elements # with their starting indices vect = [] # Push the pairs {arr[i], i+1} # into the vector of pairs vect for i in range (N): # Update vect vect.append([arr[i], i + 1 ]) # Populate the array temp[] for i in range (N): # Update temp[arr[i]] temp[arr[i]] = jump[i] # Sort the vector in # the ascending order vect.sort(reverse = False ) for i in range (N): # Jump till the previous # index <= current index while (vect[i][ 1 ] < = vect[i - 1 ][ 1 ]): # Update vect[i] vect[i] = [vect[i][ 0 ], vect[i][ 1 ] + temp[vect[i][ 0 ]]] # Increment the # number of jumps jumps + = 1 # Print the minimum number # of jumps required print (jumps) # Driver Code if __name__ = = '__main__' : # Input arr = [ 3 , 2 , 1 ] jump = [ 1 , 1 , 1 ] # Size of the array N = len (arr) minJumps(arr, jump, N) # This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for the above approach using System; using System.Linq; using System.Collections.Generic; class GFG { public class pair { public int first, second; public pair( int first, int second) { this .first = first; this .second = second; } } // Function to count minimum number // of jumps required to sort the array static void minJumps( int [] arr, int [] jump, int N) { // Stores minimum number of jumps int jumps = 0; // Stores distances of jumps int [] temp= new int [1000]; // Stores the array elements // with their starting indices List<pair> vect; vect = new List<pair>(); // Push the pairs {arr[i], i+1} // into the vector of pairs vect for ( int i = 0; i < N; i++) { // Update vect vect.Add( new pair(arr[i], i+1)); } // Populate the array temp[] for ( int i = 0; i < N; i++) { // Update temp[arr[i]] temp[arr[i]] = jump[i]; } // Sort the vector in // the ascending order vect.Sort((a, b) => { if (a.first != b.first) return a.first - b.first; return a.second - b.second; }); for ( int i = 1; i < N; i++) { // Jump till the previous // index <= current index while (vect[i].second <= vect[i - 1].second) { // Update vect[i] vect[i].first = vect[i].first; vect[i].second = vect[i].second + temp[vect[i].first]; // Increment the // number of jumps jumps++; } } // Print the minimum number // of jumps required Console.WriteLine(jumps); } // Driver program public static void Main() { // Input int [] arr = new int [] { 3, 2, 1 }; int [] jump = new int [] { 1, 1, 1 }; // Size of the array int N = arr.Length; minJumps(arr, jump, N); } } // This code is contributed by Pushpesh Raj. |
Javascript
<script> // JavaScript program for the above approach // Function to count minimum number // of jumps required to sort the array const minJumps = (arr, jump, N) => { // Stores minimum number of jumps let jumps = 0; // Stores distances of jumps let temp = new Array(1000).fill(0); // Stores the array elements // with their starting indices let vect = []; // Push the pairs {arr[i], i+1} // into the vector of pairs vect for (let i = 0; i < N; i++) { // Update vect vect.push([arr[i], i + 1]); } // Populate the array temp[] for (let i = 0; i < N; i++) { // Update temp[arr[i]] temp[arr[i]] = jump[i]; } // Sort the vector in // the ascending order vect.sort(); for (let i = 1; i < N; i++) { // Jump till the previous // index <= current index while (vect[i][1] <= vect[i - 1][1]) { // Update vect[i] vect[i] = [vect[i][0], vect[i][1] + temp[vect[i][0]]]; // Increment the // number of jumps jumps++; } } // Print the minimum number // of jumps required document.write(`${jumps}<br/>`); } // Driver Code // Input let arr = [3, 2, 1]; let jump = [1, 1, 1]; // Size of the array let N = arr.length; minJumps(arr, jump, N); // This code is contributed by rakeshsahni </script> |
6
Time Complexity: O(N2)
Auxiliary Space: O(N)
Please Login to comment...