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 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:
Â
Â
Â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++
// 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
// 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
# 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#
// 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 |
Javascript
<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)
Â
Another approach : Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memorization(top-down) because memorization method needs extra stack space of recursion calls.
Steps to solve this problem :
- Create a 2D dynamic array dp of size (N+1) x (maxE+1) and initialize it with zeros.
- Use a nested loop to iterate through the remaining rows and columns of dp.
- Inside the inner loop, update ans by finding the minimum value between ans and dp[i-1][k] + abs(arr[i-1] – k), where k is the current index in the inner loop.
- Store the value of ans in dp[i][j]
- After the loops complete, return the value of dp[N][maxE]
Implementation :
C++
// C++ program of the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
int minimumIncDec(int arr[], int N,                int maxE, int minE){    int dp[N+1][maxE+1];    memset(dp, 0, sizeof(dp));         // If only one element is present,    // then arr[] is sorted    for(int j=0;j<=maxE;j++){        dp[0][j] = 0;    }         // Iterate from minE to maxE which    // placed at previous index    for(int i=1;i<=N;i++){        for(int j=minE;j<=maxE;j++){            int ans = INT_MAX;            for(int k=minE;k<=j;k++){                // Update the answer according to                // recurrence relation                ans = min(ans, dp[i-1][k] + abs(arr[i-1] - k));            }                         // store computation of subproblem            dp[i][j] = ans;        }    }         // Return the final result    return dp[N][maxE];}Â
// Driver Codeint main(){Â Â Â Â int arr[] = {1,2,3,4};Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â
    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
// Java program of the above approachimport java.util.Arrays;Â
public class Main {Â Â public static int minimumIncDec(int[] arr, int N, int maxE, int minE) {Â Â Â Â int[][] dp = new int[N+1][maxE+1];Â Â Â Â for (int[] row : dp) {Â Â Â Â Â Â Arrays.fill(row, 0);Â Â Â Â }Â
    // If only one element is present,    // then arr[] is sorted    for (int j = 0; j <= maxE; j++) {      dp[0][j] = 0;    }Â
    // Iterate from minE to maxE which    // placed at previous index    for (int i = 1; i <= N; i++) {      for (int j = minE; j <= maxE; j++) {        int ans = Integer.MAX_VALUE;        for (int k = minE; k <= j; k++) {Â
          // Update the answer according to          // recurrence relation          ans = Math.min(ans, dp[i-1][k] + Math.abs(arr[i-1] - k));        }        // store computation of subproblem        dp[i][j] = ans;      }    }    // Return the final result    return dp[N][maxE];  }Â
  // Driver Code  public static void main(String[] args) {    int[] arr = {1,2,3,4};    int N = arr.length;    int minE = Arrays.stream(arr).min().getAsInt();    int maxE = Arrays.stream(arr).max().getAsInt();    // Function Call    System.out.println(minimumIncDec(arr, N, maxE, minE));  }}Â
// This code is contributed by shiv1o43g |
Python3
import sysÂ
def minimum_inc_dec(arr):    N = len(arr)    maxE = max(arr)    minE = min(arr)         dp = [[0] * (maxE + 1) for _ in range(N + 1)]         # If only one element is present, then arr[] is sorted    for j in range(maxE + 1):        dp[0][j] = 0         # Iterate from minE to maxE which placed at previous index    for i in range(1, N + 1):        for j in range(minE, maxE + 1):            ans = sys.maxsize            for k in range(minE, j + 1):                # Update the answer according to recurrence relation                ans = min(ans, dp[i - 1][k] + abs(arr[i - 1] - k))                         # store computation of subproblem            dp[i][j] = ans         # Return the final result    return dp[N][maxE]Â
# Driver Codearr = [1, 2, 3, 4]result = minimum_inc_dec(arr)print(result) |
Javascript
function minimumIncDec(arr, N, maxE, minE) {Â Â Â Â let dp = Array.from(Array(N + 1), () => Array(maxE + 1).fill(0));Â
    // If only one element is present,    // then arr[] is sorted    for (let j = 0; j <= maxE; j++) {        dp[0][j] = 0;    }Â
    // Iterate from minE to maxE which    // placed at previous index    for (let i = 1; i <= N; i++) {        for (let j = minE; j <= maxE; j++) {            let ans = Infinity;            for (let k = minE; k <= j; k++) {                // Update the answer according to                // recurrence relation                ans = Math.min(ans, dp[i - 1][k] + Math.abs(arr[i - 1] - k));            }Â
            // store computation of subproblem            dp[i][j] = ans;        }    }Â
    // Return the final result    return dp[N][maxE];}Â
// Driver Codelet arr = [1, 2, 3, 4];let N = arr.length;Â
let minE = Math.min(...arr);let maxE = Math.max(...arr);Â
// Function Callconsole.log(minimumIncDec(arr, N, maxE, minE)); |
Output:
0
Time Complexity: O(N^3)Â
Auxiliary Space: Â O(N*maxE)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



