Minimum number of jumps required to sort the given array in ascending order | Java program for the above approach ; Function to count minimum number of jumps required to sort the array ; Stores minimum number of jumps ; Stores distances of jumps ; Stores the array elements with their starting indices ; Push the pairs { arr [ i ] , i + 1 } into the vector of pairs vect ; Update vect ; Populate the array temp [ ] ; Update temp [ arr [ i ] ] ; Sort the vector in the ascending order ; Jump till the previous index <= current index ; Update vect [ i ] ; Increment the number of jumps ; Print the minimum number of jumps required ; Driver Code ; Input ; Size of the array
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 ) ; } }