Minimum steps to convert an Array into permutation of numbers from 1 to N | Function to find minimum number of steps to convert a given sequence into a permutation ; Sort the given array ; To store the required minimum number of operations ; Find the operations on each step ; Return the answer ; Driver code ; Function call
function get_permutation ( arr , n ) { arr . sort ( ) ; var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { result += Math . abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } var arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] ; var n = arr . length ; document . write ( get_permutation ( arr , n ) ) ;