# Minimum cost to make an Array a permutation of first N natural numbers

Given an array arr of positive integers of size N, the task is to find the minimum cost to make this array a permutation of first N natural numbers, where the cost of incrementing or decrementing an element by 1 is 1.

Examples:

Input: arr[] = {1, 1, 7, 4}
Output: 5
Explanation:
Perform increment operation on 1 one time
Perform decrement operation on 7 four times
Resultant array = {1, 2, 3, 4}

Input: arr[] = {1, 2, 3, 4, 5}
Output: 0
Explanation:
The array is already a permutation.

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

Approach:

1. Sort the array element in increasing order
2. Traverse the sorted array:
• Check if the element at ith index (0 ≤ i < N) is equals to i + 1.
• If not, then make it equal and inremenent the difference between the two as the cost of this operation.
3. When the traversal is complete, print the total cost of the operations performed.

Below is the implementation of the above approach:

## C++

 `// C++ program to calculate minimum cost ` `// to make an Array a permutation ` `// of first N natural numbers ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate minimum cost ` `// for making permutation of size N ` `int` `make_permutation(``int` `arr[], ``int` `n) ` `{ ` `    ``// sorting the array in ascending order ` `    ``sort(arr, arr + n); ` ` `  `    ``// To store the required answer ` `    ``int` `ans = 0; ` ` `  `    ``// Traverse the whole array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``ans += ``abs``(i + 1 - arr[i]); ` ` `  `    ``// Return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 3, 8, 1, 1 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``// Function call ` `    ``cout << make_permutation(arr, n); ` `} `

## Java

 `// Java program to calculate minimum cost ` `// to make an Array a permutation ` `// of first N natural numbers ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to calculate minimum cost ` `// for making permutation of size N ` `static` `int` `make_permutation(``int` `arr[], ``int` `n) ` `{ ` `    ``// sorting the array in ascending order ` `    ``Arrays.sort(arr); ` `  `  `    ``// To store the required answer ` `    ``int` `ans = ``0``; ` `  `  `    ``// Traverse the whole array ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``ans += Math.abs(i + ``1` `- arr[i]); ` `  `  `    ``// Return the required answer ` `    ``return` `ans; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``5``, ``3``, ``8``, ``1``, ``1` `}; ` `    ``int` `n = arr.length; ` `  `  `    ``// Function call ` `    ``System.out.print(make_permutation(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 program to calculate minimum cost  ` `# to make an Array a permutation  ` `# of first N natural numbers  ` ` `  `# Function to calculate minimum cost  ` `# for making permutation of size N  ` `def` `make_permutation(arr,  n) : ` ` `  `    ``# sorting the array in ascending order  ` `    ``arr.sort();  ` ` `  `    ``# To store the required answer  ` `    ``ans ``=` `0``;  ` ` `  `    ``# Traverse the whole array  ` `    ``for` `i ``in` `range``(n) : ` `        ``ans ``+``=` `abs``(i ``+` `1` `-` `arr[i]);  ` ` `  `    ``# Return the required answer  ` `    ``return` `ans;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[ ``5``, ``3``, ``8``, ``1``, ``1` `];  ` `    ``n ``=` `len``(arr);  ` ` `  `    ``# Function call  ` `    ``print``(make_permutation(arr, n));  ` ` `  `    ``# This code is contributed by Yash_R `

## C#

 `// C# program to calculate minimum cost ` `// to make an Array a permutation ` `// of first N natural numbers ` `using` `System; ` ` `  `class` `GFG{ ` `  `  `    ``// Function to calculate minimum cost ` `    ``// for making permutation of size N ` `    ``static` `int` `make_permutation(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``// sorting the array in ascending order ` `        ``Array.Sort(arr); ` `      `  `        ``// To store the required answer ` `        ``int` `ans = 0; ` `      `  `        ``// Traverse the whole array ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``ans += Math.Abs(i + 1 - arr[i]); ` `      `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `Main(``string``[] args) ` `    ``{ ` `        ``int` `[]arr = { 5, 3, 8, 1, 1 }; ` `        ``int` `n = arr.Length; ` `      `  `        ``// Function call ` `        ``Console.WriteLine(make_permutation(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Yash_R `

Output:

```5
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.

Improved By : Rajput-Ji, Yash_R