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.
- 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 naive approach for solution is mentioned in the Set-1 of this problem. To determine the number of jumps for each element here the help of map is taken. For the current element determine the previous element which will be there in sorted order and then determine number of jumps required to put the current element after that one. Follow the steps mentioned below:
- Store the current position of each element in a map.
- Store the elements in sorted order in a set.
- Find the difference of position of the current element with its previous element in sorted order. Then find the number of jumps required by dividing the difference with the length of jump of current one.
- Add this count to the final answer.
- Return the final answer.
Below is the implementation of the above approach.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to return minimum required jumps int minJumps( vector< int > arr, vector< int > jump, int N) { int temp[N]; for ( int i = 0; i < N; i++) temp[i] = arr[i]; sort(temp, temp + N); unordered_map< int , int > a; for ( int i = 0; i < N; i++) { a[arr[i]] = i; } int ans = 0; int x = 1, y = 0; while (x < N) { if (a[temp[x]] <= a[temp[y]]) { int jumps = ceil ((a[temp[y]] - a[temp[x]] + 1) / jump[a[temp[x]]]); ans += jumps; a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]]; } x++; y++; } return ans; } // Driver code int main() { int N = 3; vector< int > arr = {3, 2, 1}; vector< int > jump = {1, 1, 1}; cout << (minJumps(arr, jump, N)); return 0; } // This code is contributed by lokeshpotta20. |
Java
// Java code to implement the above approach import java.io.*; import java.util.*; class GFG { // Function to return minimum required jumps public static int minJumps( int arr[], int jump[], int N) { int temp[] = new int [N]; for ( int i = 0 ; i < N; i++) temp[i] = arr[i]; Arrays.sort(temp); HashMap<Integer, Integer> a = new HashMap<>(); for ( int i = 0 ; i < N; i++) { a.put(arr[i], i); } int ans = 0 ; int x = 1 , y = 0 ; while (x < N) { if (a.get(temp[x]) <= a.get(temp[y])) { int jumps = ( int )Math.ceil( ( float )(a.get(temp[y]) - a.get(temp[x]) + 1 ) / jump[a.get(temp[x])]); ans += jumps; a.put(temp[x], a.get(temp[x]) + jumps * jump[a.get(temp[x])]); } x++; y++; } return ans; } // Driver code public static void main(String[] args) { int N = 3 ; int arr[] = { 3 , 2 , 1 }; int jump[] = { 1 , 1 , 1 }; System.out.println(minJumps(arr, jump, N)); } } |
Python3
# Python program for the above approach import math as Math # Function to return minimum required jumps def minJumps(arr, jump, N): temp = [ 0 ] * N for i in range (N): temp[i] = arr[i] temp.sort() a = {} for i in range (N): a[arr[i]] = i ans = 0 x = 1 y = 0 while x < N: if a[temp[x]] < = a[temp[y]]: jumps = Math.ceil((a[temp[y]] - a[temp[x]] + 1 ) / jump[a[temp[x]]]) ans + = jumps a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]] x + = 1 y + = 1 return ans # Driver code N = 3 arr = [ 3 , 2 , 1 ] jump = [ 1 , 1 , 1 ] print (minJumps(arr, jump, N)) # This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to return minimum required jumps public static int minJumps( int [] arr, int [] jump, int N) { int [] temp = new int [N]; for ( int i = 0; i < N; i++) temp[i] = arr[i]; Array.Sort(temp); Dictionary< int , int > a = new Dictionary< int , int >(); for ( int i = 0; i < N; i++) { a[arr[i]] = i; } int ans = 0; int x = 1, y = 0; while (x < N) { if (a[temp[x]] <= a[temp[y]]) { int jumps = ( int )Math.Ceiling( ( float )(a[temp[y]] - a[temp[x]] + 1) / jump[a[temp[x]]]); ans += jumps; a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]]; } x++; y++; } return ans; } // Driver code public static void Main( string [] args) { int N = 3; int [] arr = { 3, 2, 1 }; int [] jump = { 1, 1, 1 }; Console.Write(minJumps(arr, jump, N)); } } // This code is contributed by ukasp |
Javascript
<script> // JavaScript program for the above approach // Function to return minimum required jumps const minJumps = (arr, jump, N) => { let temp = new Array(N).fill(0); for (let i = 0; i < N; i++) temp[i] = arr[i]; temp.sort(); let a = {}; for (let i = 0; i < N; i++) { a[arr[i]] = i; } let ans = 0; let x = 1, y = 0; while (x < N) { if (a[temp[x]] <= a[temp[y]]) { let jumps = Math.ceil((a[temp[y]] - a[temp[x]] + 1) / jump[a[temp[x]]]); ans += jumps; a[temp[x]] = a[temp[x]] + jumps * jump[a[temp[x]]]; } x++; y++; } return ans; } // Driver code let N = 3; let arr = [3, 2, 1]; let jump = [1, 1, 1]; document.write(minJumps(arr, jump, N)); // This code is contributed by rakeshsahni </script> |
6
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!