Minimum number of jumps required to sort the given array in ascending order | 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
def minJumps ( arr , jump , N ) : NEW_LINE INDENT jumps = 0 NEW_LINE temp = [ 0 for i in range ( 1000 ) ] NEW_LINE vect = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT vect . append ( [ arr [ i ] , i + 1 ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT temp [ arr [ i ] ] = jump [ i ] NEW_LINE DEDENT vect . sort ( reverse = False ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT while ( vect [ i ] [ 1 ] <= vect [ i - 1 ] [ 1 ] ) : NEW_LINE INDENT vect [ i ] = [ vect [ i ] [ 0 ] , vect [ i ] [ 1 ] + temp [ vect [ i ] [ 0 ] ] ] NEW_LINE jumps += 1 NEW_LINE DEDENT DEDENT print ( jumps ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 ] NEW_LINE jump = [ 1 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minJumps ( arr , jump , N ) NEW_LINE DEDENT