# Minimum increment or decrement required to sort the array | Top-down Approach

Given an array arr[] of N integers, the task is to sort the array in increasing order by performing minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.

Examples:

Input: arr[] = {5, 4, 3, 2, 1}
Output: 6
Explanation:
The sorted array of arr[] is {3, 3, 3, 3, 3}
Therefore the minimum increments/decrement are:
At index 0, 5 – 3 = 2 (decrement 2)
At index 1, 4 – 3 = 1 (decrement 1)
At index 3, 2 + 1 = 3 (increment 1)
At index 4, 1 + 2 = 3 (increment 2)
The total increment/decrement is 2 + 1 + 1 + 2 = 6.

Input: arr[] = {1, 2, 3, 4}
Output: 0
Explanation:

Bottom-up Approach: This problem can be solved using Dynamic Programming. A Bottom-up Approach to this problem statement is discussed in this article.

Bottom-up Approach: Here we will use Top-down Dynamic Programming to solve this problem.

Let 2D array (say `dp[i][j]`) used to store the upto index i where last element is at index j.

Below are the steps:

1. To make the array element in sorted by using the given operations, we know that an element cannot become greater than the maximum value of the array and less than the minimum value of the array(say m) by increment or decrement.
2. Therefore, Fix an element(say X) at ith position, then (i-1)th position value(say Y) can be in the range [m, X].
3. Keep placing the smaller element less than or equals to arr[i] at (i-1)th position for every index i of arr[] and calculate the minimum increment or decrement by adding abs(arr[i] – Y).
4. Therefore the recurrence relation for the above mentioned approach can be written as:

``` dp[i][j] = min(dp[i][j], abs(arr[i] - Y) + recursive_function(i-1, Y)) ```
where m ≤ Y ≤ arr[j].

Below is the implementation of the above approach:

 `// C++ program of the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Dp array to memoized ` `// the value recursive call ` `int` `dp[1000][1000]; ` ` `  `// Function to find the minimum increment ` `// or decrement needed to make the array ` `// sorted ` `int` `minimumIncDec(``int` `arr[], ``int` `N, ` `                  ``int` `maxE, ``int` `minE) ` `{ ` `    ``// If only one element is present, ` `    ``// then arr[] is sorted ` `    ``if` `(N == 0) { ` `        ``return` `0; ` `    ``} ` ` `  `    ``// If dp[N][maxE] is precalculated, ` `    ``// then return the result ` `    ``if` `(dp[N][maxE]) ` `        ``return` `dp[N][maxE]; ` ` `  `    ``int` `ans = INT_MAX; ` ` `  `    ``// Iterate from minE to maxE which ` `    ``// placed at previous index ` `    ``for` `(``int` `k = minE; k <= maxE; k++) { ` ` `  `        ``// Update the answer according to ` `        ``// recurrence relation ` `        ``int` `x = minimumIncDec( ` `            ``arr, N - 1, k, minE); ` `        ``ans = min( ` `            ``ans, ` `            ``x + ``abs``(arr[N - 1] - k)); ` `    ``} ` ` `  `    ``// Memoized the value ` `    ``// for dp[N][maxE] ` `    ``dp[N][maxE] = ans; ` ` `  `    ``// Return the final result ` `    ``return` `dp[N][maxE]; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 4, 3, 2, 1 }; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``// Find the minimum and maximum ` `    ``// element from the arr[] ` `    ``int` `minE = *min_element(arr, arr + N); ` `    ``int` `maxE = *max_element(arr, arr + N); ` ` `  `    ``// Function Call ` `    ``cout << minimumIncDec( ` `        ``arr, N, maxE, minE); ` `    ``return` `0; ` `} `

Output:

```6
```

Time Complexity: O(N2)
Auxiliary Space: O(N2)

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.