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

29 Oct, 2021
Given an array arr[] of N integers, the task is to sort the array in increasing order by performing a 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: The array is already sorted.
Bottom-up Approach: This problem can be solved using Dynamic Programming. A Bottom-up Approach to this problem statement is discussed in this article. Top-Down 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:  
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.Therefore, Fix an element(say X) at ith position, then (i-1)th position value(say Y) can be in the range [m, X].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).Therefore the recurrence relation for the above mentioned approach can be written as: 
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.
Therefore, Fix an element(say X) at ith position, then (i-1)th position value(say Y) can be in the range [m, X].
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).
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++
Java
Python3
C#
Javascript
// C++ program of the above approach #include <bits/stdc++.h>using namespace std; // Dp array to memoized// the value recursive callint dp[1000][1000]; // Function to find the minimum increment// or decrement needed to make the array// sortedint 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 Codeint 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;}
// Java program of the above approachimport java.util.*; class GFG{ // Dp array to memoized// the value recursive callstatic int [][]dp = new int[1000][1000]; // Function to find the minimum increment// or decrement needed to make the array// sortedstatic 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] != 0)        return dp[N][maxE];     int ans = Integer.MAX_VALUE;     // 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 = Math.min(ans,                       x + Math.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 Codepublic static void main(String[] args){    int arr[] = { 5, 4, 3, 2, 1 };    int N = arr.length;     // Find the minimum and maximum    // element from the arr[]    int minE = Arrays.stream(arr).min().getAsInt();    int maxE = Arrays.stream(arr).max().getAsInt();     // Function call    System.out.print(minimumIncDec(        arr, N, maxE, minE));}} // This code is contributed by amal kumar choubey
# Python3 program of the above approachimport sys # Dp array to memoized# the value recursive calldp = [[ 0 for x in range(1000)]          for y in range(1000)] # Function to find the minimum increment# or decrement needed to make the array# sorteddef minimumIncDec(arr, N, maxE, 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]     ans = sys.maxsize     # Iterate from minE to maxE which    # placed at previous index    for k in range(minE, maxE + 1):         # Update the answer according to        # recurrence relation        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 Codeif __name__ == "__main__":     arr = [ 5, 4, 3, 2, 1 ]    N = len(arr)     # Find the minimum and maximum    # element from the arr[]    minE = min(arr)    maxE = max(arr)     # Function Call    print(minimumIncDec(arr, N, maxE, minE)) # This code is contributed by chitranayal
// C# program of the above approachusing System;using System.Linq; class GFG{ // Dp array to memoized// the value recursive callstatic int [,]dp = new int[1000, 1000]; // Function to find the minimum increment// or decrement needed to make the array// sortedstatic 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] != 0)        return dp[N, maxE];     int ans = int.MaxValue;     // 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 = Math.Min(ans,                       x + Math.Abs(arr[N - 1] - k));    }     // Memoized the value    // for dp[N,maxE]    dp[N, maxE] = ans;     // Return the readonly result    return dp[N,maxE];} // Driver Codepublic static void Main(String[] args){    int []arr = { 5, 4, 3, 2, 1 };    int N = arr.Length;     // Find the minimum and maximum    // element from the []arr    int minE = arr.Min();    int maxE = arr.Max();     // Function call    Console.Write(minimumIncDec(arr, N,                                maxE, minE));}} // This code is contributed by Rohit_ranjan
<script> // JavaScript program of the above approach // Dp array to memoized// the value recursive calllet dp = new Array(); for(let i = 0; i < 1000; i++){    let temp = [];    for(let j = 0; j < 1000; j++){        temp.push([])    }    dp.push(temp)} // Function to find the minimum increment// or decrement needed to make the array// sorted function minimumIncDec(arr, N, maxE, 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];     let ans = Number.MAX_SAFE_INTEGER;     // Iterate from minE to maxE which    // placed at previous index    for (let k = minE; k <= maxE; k++) {         // Update the answer according to        // recurrence relation        let x = minimumIncDec(arr, N - 1, k, minE);        ans = Math.min(ans,x + Math.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     let arr = [ 5, 4, 3, 2, 1 ];    let N = arr.length;     // Find the minimum and maximum    // element from the arr[]    let minE = arr.sort((a, b) => a - b)[0];    let maxE = arr.sort((a, b) => b - a)[0];     // Function Call    document.write(minimumIncDec(arr, N, maxE, minE)); // This code is contributed by _saurabh_jaiswal </script>
6
 
Time Complexity: O(N*maxE) Auxiliary Space: O(N2) 
VikasVishwakarma1
ukasp
Amal Kumar Choubey
Rohit_ranjan
_saurabh_jaiswal
sugalialnaikkece17
Algorithms
Arrays
Dynamic Programming
Recursion
Sorting
Arrays
Dynamic Programming
Recursion
Sorting
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
How to write a Pseudo Code?
Understanding Time Complexity with Simple Examples
Introduction to Algorithms
Arrays in Java
Arrays in C/C++
Maximum and minimum of an array using minimum number of comparisons
Write a program to reverse an array or string
Program for array rotation