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++
#include <bits/stdc++.h>
using namespace std;
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;
}
int main()
{
int N = 3;
vector< int > arr = {3, 2, 1};
vector< int > jump = {1, 1, 1};
cout << (minJumps(arr, jump, N));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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;
}
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
import math as Math
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
N = 3
arr = [ 3 , 2 , 1 ]
jump = [ 1 , 1 , 1 ]
print (minJumps(arr, jump, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
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;
}
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));
}
}
|
Javascript
<script>
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;
}
let N = 3;
let arr = [3, 2, 1];
let jump = [1, 1, 1];
document.write(minJumps(arr, jump, N));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Jan, 2022
Like Article
Save Article