Minimum steps to convert an Array into permutation of numbers from 1 to N | C # program to find minimum number of steps to convert a given sequence into a permutation ; 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
using System ; class GFG { static int get_permutation ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result += Math . Abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . Length ; Console . Write ( get_permutation ( arr , n ) ) ; } }