# Minimum steps to convert an Array into permutation of numbers from 1 to N

Given an array arr of length N, the task is to count the minimum number of operations to convert given sequence into a permutation of first N natural numbers (1, 2, …., N). In each operation, increment or decrement an element by one.

Examples:

Input: arr[] = {4, 1, 3, 6, 5}
Output: 4
Apply decrement operation four times on 6

Input : arr[] = {0, 2, 3, 4, 1, 6, 8, 9}
Output : 7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: An efficient approach is to sort the given array and for each element, find the difference between the arr[i] and i(1 based indexing). Find the sum of all such difference, and this will be the minimum steps required.

Below is the implementation of the above approach:

## CPP

 `// C++ program to find minimum number of steps to ` `// convert a given sequence into a permutation ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find minimum number of steps to ` `// convert a given sequence into a permutation ` `int` `get_permutation(``int` `arr[], ``int` `n) ` `{ ` `    ``// Sort the given array ` `    ``sort(arr, arr + n); ` ` `  `    ``// To store the required minimum ` `    ``// number of operations ` `    ``int` `result = 0; ` ` `  `    ``// Find the operations on each step ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``result += ``abs``(arr[i] - (i + 1)); ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Function call ` `    ``cout << get_permutation(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find minimum number of steps to ` `// convert a given sequence into a permutation ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to find minimum number of steps to ` `// convert a given sequence into a permutation ` `static` `int` `get_permutation(``int` `arr[], ``int` `n) ` `{ ` `    ``// Sort the given array ` `    ``Arrays.sort(arr); ` `  `  `    ``// To store the required minimum ` `    ``// number of operations ` `    ``int` `result = ``0``; ` `  `  `    ``// Find the operations on each step ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `        ``result += Math.abs(arr[i] - (i + ``1``)); ` `    ``} ` `  `  `    ``// Return the answer ` `    ``return` `result; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``0``, ``2``, ``3``, ``4``, ``1``, ``6``, ``8``, ``9` `}; ` `  `  `    ``int` `n = arr.length; ` `  `  `    ``// Function call ` `    ``System.out.print(get_permutation(arr, n)); ` `  `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 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 ` `def` `get_permutation(arr, n): ` ` `  `    ``# Sort the given array ` `    ``arr ``=` `sorted``(arr) ` ` `  `    ``# To store the required minimum ` `    ``# number of operations ` `    ``result ``=` `0` ` `  `    ``# Find the operations on each step ` `    ``for` `i ``in` `range``(n): ` `        ``result ``+``=` `abs``(arr[i] ``-` `(i ``+` `1``)) ` ` `  `    ``# Return the answer ` `    ``return` `result ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr``=``[``0``, ``2``, ``3``, ``4``, ``1``, ``6``, ``8``, ``9``] ` `    ``n ``=` `len``(arr) ` ` `  `    ``# Function call ` `    ``print``(get_permutation(arr, n)) ` ` `  `# This code is contributed by mohit kumar 29     `

## C#

 `// C# program to find minimum number of steps to ` `// convert a given sequence into a permutation ` `using` `System; ` `  `  `class` `GFG{ ` ` `  `// Function to find minimum number of steps to ` `// convert a given sequence into a permutation ` `static` `int` `get_permutation(``int` `[]arr, ``int` `n) ` `{ ` `    ``// Sort the given array ` `    ``Array.Sort(arr); ` ` `  `    ``// To store the required minimum ` `    ``// number of operations ` `    ``int` `result = 0; ` ` `  `    ``// Find the operations on each step ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``result += Math.Abs(arr[i] - (i + 1)); ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `result; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = { 0, 2, 3, 4, 1, 6, 8, 9 }; ` ` `  `    ``int` `n = arr.Length; ` ` `  `    ``// Function call ` `    ``Console.Write(get_permutation(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by shivanisinghss2110 `

Output:

```7
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.