# Minimum number of increment/decrement operations such that array contains all elements from 1 to N

Given an array of N elements, the task is to convert it into a permutation (Each number from 1 to N occurs exactly once) by using the following operations a minimum number of times:

- Increment any number.
- Decrement any number.

**Examples:**

Input:arr[] = {1, 1, 4}Output:2 The array can be converted into [1, 2, 3] by adding 1 to the 1st index i.e. 1 + 1 = 2 and decrementing 2nd index by 1 i.e. 4- 1 = 3Input:arr[] = {3, 0}Output:2 The array can be converted into [2, 1]

**Approach:** To minimize the number of moves/operations, sort the given array and make a[i] = i+1 (0-based) which will take **abs(i+1-a[i])** no. of operations for each element.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum operations ` `long` `long` `minimumMoves(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `long` `long` `operations = 0; ` ` ` ` ` `// Sort the given array ` ` ` `sort(a, a + n); ` ` ` ` ` `// Count operations by assigning a[i] = i+1 ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `operations += ` `abs` `(a[i] - (i + 1)); ` ` ` ` ` `return` `operations; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 3, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << minimumMoves(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `import` `java.util.*; ` `class` `solution ` `{ ` `// Function to find the minimum operations ` `static` `long` `minimumMoves(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `long` `operations = ` `0` `; ` ` ` ` ` `// Sort the given array ` ` ` `Arrays.sort(a); ` ` ` ` ` `// Count operations by assigning a[i] = i+1 ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `operations += (` `long` `)Math.abs(a[i] - (i + ` `1` `)); ` ` ` ` ` `return` `operations; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `5` `, ` `3` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(minimumMoves(arr, n)); ` ` ` `} ` ` ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the above approach ` ` ` `# Function to find the minimum operations ` `def` `minimumMoves(a, n): ` ` ` ` ` `operations ` `=` `0` ` ` `# Sort the given array ` ` ` `a.sort(reverse ` `=` `False` `) ` ` ` ` ` `# Count operations by assigning a[i] = i+1 ` ` ` `for` `i ` `in` `range` `(` `0` `,n,` `1` `): ` ` ` `operations ` `=` `operations ` `+` `abs` `(a[i] ` `-` `(i ` `+` `1` `)) ` ` ` ` ` `return` `operations ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[ ` `5` `, ` `3` `, ` `2` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `print` `(minimumMoves(arr, n)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to find the minimum operations ` `static` `long` `minimumMoves(` `int` `[]a, ` `int` `n) ` `{ ` ` ` ` ` `long` `operations = 0; ` ` ` ` ` `// Sort the given array ` ` ` `Array.Sort(a); ` ` ` ` ` `// Count operations by assigning ` ` ` `// a[i] = i+1 ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `operations += (` `long` `)Math.Abs(a[i] - (i + 1)); ` ` ` ` ` `return` `operations; ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `[]arr = { 5, 3, 2 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(minimumMoves(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Sach_Code ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` `// Function to find the minimum operations ` ` ` `function` `minimumMoves(` `$a` `, ` `$n` `) ` `{ ` ` ` `$operations` `= 0; ` ` ` ` ` `// Sort the given array ` ` ` `sort(` `$a` `); ` ` ` ` ` `// Count operations by assigning ` ` ` `// a[i] = i+1 ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$operations` `+= ` `abs` `(` `$a` `[` `$i` `] - ` ` ` `(` `$i` `+ 1)); ` ` ` ` ` `return` `$operations` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 5, 3, 2 ); ` `$n` `= sizeof(` `$arr` `); ` ` ` `echo` `minimumMoves(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity: ** O(NlogN)

## Recommended Posts:

- Minimum number of operations on an array to make all elements 0
- Minimum number of increment-other operations to make all array elements equal.
- Find the minimum number of operations required to make all array elements equal
- Minimum gcd operations to make all array elements one
- Minimum delete operations to make all elements of array same
- Minimum no. of operations required to make all Array Elements Zero
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum operations required to make all the array elements equal
- Minimum operations required to make all the elements distinct in an array
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Minimum number of given operations required to reduce the array to 0 element
- Number of array elements derivable from D after performing certain operations

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.