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++
#include <bits/stdc++.h>
using namespace std;
void minJumps( int arr[], int jump[], int N)
{
int jumps = 0;
int temp[1000];
vector<pair< int , int > > vect;
for ( int i = 0; i < N; i++) {
vect.push_back({ arr[i], i + 1 });
}
for ( int i = 0; i < N; i++) {
temp[arr[i]] = jump[i];
}
sort(vect.begin(), vect.end());
for ( int i = 1; i < N; i++) {
while (vect[i].second <= vect[i - 1].second) {
vect[i] = make_pair(vect[i].first,
vect[i].second
+ temp[vect[i].first]);
jumps++;
}
}
cout << jumps << endl;
}
int main()
{
int arr[] = { 3, 2, 1 };
int jump[] = { 1, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
minJumps(arr, jump, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void minJumps( int arr[], int jump[], int N)
{
int jumps = 0 ;
int temp[] = new int [ 1000 ];
int vect[][] = new int [N][ 2 ];
for ( int i = 0 ; i < N; i++)
{
vect[i][ 0 ] = arr[i];
vect[i][ 1 ] = i + 1 ;
}
for ( int i = 0 ; i < N; i++)
{
temp[arr[i]] = jump[i];
}
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++)
{
while (vect[i][ 1 ] <= vect[i - 1 ][ 1 ])
{
vect[i][ 0 ] = vect[i][ 0 ];
vect[i][ 1 ] = vect[i][ 1 ] + temp[vect[i][ 0 ]];
jumps++;
}
}
System.out.println(jumps);
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 1 };
int jump[] = { 1 , 1 , 1 };
int N = arr.length;
minJumps(arr, jump, N);
}
}
|
Python3
def minJumps(arr, jump, N):
jumps = 0
temp = [ 0 for i in range ( 1000 )]
vect = []
for i in range (N):
vect.append([arr[i], i + 1 ])
for i in range (N):
temp[arr[i]] = jump[i]
vect.sort(reverse = False )
for i in range (N):
while (vect[i][ 1 ] < = vect[i - 1 ][ 1 ]):
vect[i] = [vect[i][ 0 ], vect[i][ 1 ] +
temp[vect[i][ 0 ]]]
jumps + = 1
print (jumps)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 1 ]
jump = [ 1 , 1 , 1 ]
N = len (arr)
minJumps(arr, jump, N)
|
C#
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;
}
}
static void minJumps( int [] arr, int [] jump, int N)
{
int jumps = 0;
int [] temp= new int [1000];
List<pair> vect;
vect = new List<pair>();
for ( int i = 0; i < N; i++)
{
vect.Add( new pair(arr[i], i+1));
}
for ( int i = 0; i < N; i++) {
temp[arr[i]] = jump[i];
}
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++)
{
while (vect[i].second <= vect[i - 1].second)
{
vect[i].first = vect[i].first;
vect[i].second = vect[i].second + temp[vect[i].first];
jumps++;
}
}
Console.WriteLine(jumps);
}
public static void Main()
{
int [] arr = new int [] { 3, 2, 1 };
int [] jump = new int [] { 1, 1, 1 };
int N = arr.Length;
minJumps(arr, jump, N);
}
}
|
Javascript
<script>
const minJumps = (arr, jump, N) => {
let jumps = 0;
let temp = new Array(1000).fill(0);
let vect = [];
for (let i = 0; i < N; i++) {
vect.push([arr[i], i + 1]);
}
for (let i = 0; i < N; i++) {
temp[arr[i]] = jump[i];
}
vect.sort();
for (let i = 1; i < N; i++) {
while (vect[i][1] <= vect[i - 1][1]) {
vect[i] = [vect[i][0],
vect[i][1]
+ temp[vect[i][0]]];
jumps++;
}
}
document.write(`${jumps}<br/>`);
}
let arr = [3, 2, 1];
let jump = [1, 1, 1];
let N = arr.length;
minJumps(arr, jump, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
25 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...