Find minimum adjustment cost of an array

04 Jul, 2022
Given an array of positive integers, replace each element in the array such that the difference between adjacent elements in the array is less than or equal to a given target. We need to minimize the adjustment cost, that is the sum of differences between new and old values. We basically need to minimize ∑|A[i] – Anew[i]| where 0 ≤ i ≤ n-1, n is size of A[] and Anew[] is the array with adjacent difference less that or equal to target.
Assume all elements of the array is less than constant M = 100.
Examples: 
Input: arr = [1, 3, 0, 3], target = 1
Output: Minimum adjustment cost is 3
Explanation: One of the possible solutions 
is [2, 3, 2, 3]

Input: arr = [2, 3, 2, 3], target = 1
Output: Minimum adjustment cost is 0
Explanation:  All adjacent elements in the input 
array are already less than equal to given target

Input: arr = [55, 77, 52, 61, 39, 6, 
             25, 60, 49, 47], target = 10
Output: Minimum adjustment cost is 75
Explanation: One of the possible solutions is 
[55, 62, 52, 49, 39, 29, 30, 40, 49, 47]
In order to minimize the adjustment cost ∑|A[i] – Anew[i]| for all index i in the array, |A[i] – Anew[i]| should be as close to zero as possible. Also, |A[i] – Anew[i+1] ]| ≤ Target.This problem can be solved by dynamic programming.
Let dp[i][j] defines minimal adjustment cost on changing A[i] to j, then the DP relation is defined by – 
dp[i][j] = min{dp[i - 1][k]} + |j - A[i]|
           for all k's such that |k - j| ≤ target
Here, 0 ≤ i ≤ n and 0 ≤ j ≤ M where n is number of elements in the array and M = 100. We have to consider all k such that max(j – target, 0) ≤ k ≤ min(M, j + target)Finally, the minimum adjustment cost of the array will be min{dp[n – 1][j]} for all 0 ≤ j ≤ M.
Below is the implementation of above idea –
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find minimum adjustment cost of an array#include <bits/stdc++.h>using namespace std; #define M 100 // Function to find minimum adjustment cost of an arrayint minAdjustmentCost(int A[], int n, int target){    // dp[i][j] stores minimal adjustment cost on changing    // A[i] to j    int dp[n][M + 1];     // handle first element of array separately    for (int j = 0; j <= M; j++)        dp[0][j] = abs(j - A[0]);     // do for rest elements of the array    for (int i = 1; i < n; i++)    {        // replace A[i] to j and calculate minimal adjustment        // cost dp[i][j]        for (int j = 0; j <= M; j++)        {          // initialize minimal adjustment cost to INT_MAX          dp[i][j] = INT_MAX;           // consider all k such that k >= max(j - target, 0) and          // k <= min(M, j + target) and take minimum          for (int k = max(j-target,0); k <= min(M,j+target); k++)             dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(A[i] - j));        }    }        // return minimum value from last row of dp table    int res = INT_MAX;    for (int j = 0; j <= M; j++)        res = min(res, dp[n - 1][j]);     return res;} // Driver Program to test above functionsint main(){    int arr[] = {55, 77, 52, 61, 39, 6, 25, 60, 49, 47};    int n = sizeof(arr) / sizeof(arr[0]);    int target = 10;     cout << "Minimum adjustment cost is "         << minAdjustmentCost(arr, n, target) << endl;     return 0;}
// Java program to find minimum adjustment cost of an arrayimport java.io.*;import java.util.*; class GFG{    public static int M = 100;         // Function to find minimum adjustment cost of an array    static int minAdjustmentCost(int A[], int n, int target)    {        // dp[i][j] stores minimal adjustment cost on changing        // A[i] to j        int[][] dp = new int[n][M + 1];          // handle first element of array separately        for (int j = 0; j <= M; j++)            dp[0][j] = Math.abs(j - A[0]);          // do for rest elements of the array        for (int i = 1; i < n; i++)        {            // replace A[i] to j and calculate minimal adjustment            // cost dp[i][j]            for (int j = 0; j <= M; j++)            {                // initialize minimal adjustment cost to INT_MAX                dp[i][j] = Integer.MAX_VALUE;                  // consider all k such that k >= max(j - target, 0) and                // k <= min(M, j + target) and take minimum                int k = Math.max(j-target,0);                for ( ; k <= Math.min(M,j+target); k++)                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] +                                                 Math.abs(A[i] - j));            }        }             // return minimum value from last row of dp table        int res = Integer.MAX_VALUE;        for (int j = 0; j <= M; j++)            res = Math.min(res, dp[n - 1][j]);          return res;    }         // Driver program    public static void main (String[] args)    {        int arr[] = {55, 77, 52, 61, 39, 6, 25, 60, 49, 47};        int n = arr.length;        int target = 10;          System.out.println("Minimum adjustment cost is "                    +minAdjustmentCost(arr, n, target));    }} // This code is contributed by Pramod Kumar
# Python3 program to find minimum# adjustment cost of an arrayM = 100 # Function to find minimum# adjustment cost of an arraydef minAdjustmentCost(A, n, target):         # dp[i][j] stores minimal adjustment    # cost on changing A[i] to j    dp = [[0 for i in range(M + 1)]             for i in range(n)]                  # handle first element    # of array separately    for j in range(M + 1):        dp[0][j] = abs(j - A[0])     # do for rest elements    # of the array    for i in range(1, n):                 # replace A[i] to j and        # calculate minimal adjustment        # cost dp[i][j]        for j in range(M + 1):                         # initialize minimal adjustment            # cost to INT_MAX            dp[i][j] = 100000000                         # consider all k such that            # k >= max(j - target, 0) and            # k <= min(M, j + target) and            # take minimum            for k in range(max(j - target, 0),                           min(M, j + target) + 1):                dp[i][j] = min(dp[i][j], dp[i - 1][k] +                                        abs(A[i] - j))                                             # return minimum value from    # last row of dp table    res = 10000000    for j in range(M + 1):        res = min(res, dp[n - 1][j])    return res # Driver Codearr= [55, 77, 52, 61, 39,       6, 25, 60, 49, 47]n = len(arr)target = 10print("Minimum adjustment cost is",       minAdjustmentCost(arr, n, target),                              sep = ' ') # This code is contributed# by sahilshelangia
// C# program to find minimum adjustment// cost of an arrayusing System; class GFG {         public static int M = 100;         // Function to find minimum adjustment    // cost of an array    static int minAdjustmentCost(int []A, int n,                                     int target)    {                 // dp[i][j] stores minimal adjustment        // cost on changing A[i] to j        int[,] dp = new int[n,M + 1];         // handle first element of array        // separately        for (int j = 0; j <= M; j++)            dp[0,j] = Math.Abs(j - A[0]);         // do for rest elements of the array        for (int i = 1; i < n; i++)        {            // replace A[i] to j and calculate            // minimal adjustment cost dp[i][j]            for (int j = 0; j <= M; j++)            {                // initialize minimal adjustment                // cost to INT_MAX                dp[i,j] = int.MaxValue;                 // consider all k such that                // k >= max(j - target, 0) and                // k <= min(M, j + target) and                // take minimum                int k = Math.Max(j - target, 0);                                 for ( ; k <= Math.Min(M, j +                                   target); k++)                    dp[i,j] = Math.Min(dp[i,j],                                   dp[i - 1,k]                         + Math.Abs(A[i] - j));            }        }         // return minimum value from last        // row of dp table        int res = int.MaxValue;        for (int j = 0; j <= M; j++)            res = Math.Min(res, dp[n - 1,j]);         return res;    }         // Driver program    public static void Main ()    {        int []arr = {55, 77, 52, 61, 39,                        6, 25, 60, 49, 47};        int n = arr.Length;        int target = 10;         Console.WriteLine("Minimum adjustment"                                 + " cost is "         + minAdjustmentCost(arr, n, target));    }} // This code is contributed by Sam007.
<?php// PHP program to find minimum// adjustment cost of an array $M = 100; // Function to find minimum// adjustment cost of an arrayfunction minAdjustmentCost( $A, $n, $target){         // dp[i][j] stores minimal    // adjustment cost on changing    // A[i] to j    global $M;    $dp = array(array());     // handle first element    // of array separately    for($j = 0; $j <= $M; $j++)        $dp[0][$j] = abs($j - $A[0]);     // do for rest    // elements of the array    for($i = 1; $i < $n; $i++)    {                 // replace A[i] to j and        // calculate minimal adjustment        // cost dp[i][j]        for($j = 0; $j <= $M; $j++)        {                         // initialize minimal adjustment            // cost to INT_MAX            $dp[$i][$j] = PHP_INT_MAX;                 // consider all k such that            // k >= max(j - target, 0) and            // k <= min(M, j + target) and            // take minimum            for($k = max($j - $target, 0);                $k <= min($M, $j + $target);                                       $k++)                $dp[$i][$j] = min($dp[$i][$j],                              $dp[$i - 1][$k] +                              abs($A[$i] - $j));        }    }     // return minimum value    // from last row of dp table    $res = PHP_INT_MAX;    for($j = 0; $j <= $M; $j++)        $res = min($res, $dp[$n - 1][$j]);     return $res;}     // Driver Code    $arr = array(55, 77, 52, 61, 39,                 6, 25, 60, 49, 47);    $n = count($arr);    $target = 10;     echo "Minimum adjustment cost is "        , minAdjustmentCost($arr, $n, $target); // This code is contributed by anuj_67.?>
<script>    // Javascript program to find minimum adjustment cost of an array    let M = 100;           // Function to find minimum adjustment cost of an array    function minAdjustmentCost(A, n, target)    {             // dp[i][j] stores minimal adjustment cost on changing        // A[i] to j        let dp = new Array(n);        for (let i = 0; i < n; i++)        {            dp[i] = new Array(n);            for (let j = 0; j <= M; j++)            {                dp[i][j] = 0;            }        }            // handle first element of array separately        for (let j = 0; j <= M; j++)            dp[0][j] = Math.abs(j - A[0]);            // do for rest elements of the array        for (let i = 1; i < n; i++)        {            // replace A[i] to j and calculate minimal adjustment            // cost dp[i][j]            for (let j = 0; j <= M; j++)            {                // initialize minimal adjustment cost to INT_MAX                dp[i][j] = Number.MAX_VALUE;                    // consider all k such that k >= max(j - target, 0) and                // k <= min(M, j + target) and take minimum                let k = Math.max(j-target,0);                for ( ; k <= Math.min(M,j+target); k++)                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] +                                                 Math.abs(A[i] - j));            }        }               // return minimum value from last row of dp table        let res = Number.MAX_VALUE;        for (let j = 0; j <= M; j++)            res = Math.min(res, dp[n - 1][j]);            return res;    }         let arr = [55, 77, 52, 61, 39, 6, 25, 60, 49, 47];    let n = arr.length;    let target = 10;     document.write("Minimum adjustment cost is "                       +minAdjustmentCost(arr, n, target));     // This code is contributed by decode2207.</script>
Minimum adjustment cost is 75

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
Sam007
vt_m
sahilshelangia
shubham_singh
decode2207
hardikkoriintern
Arrays
Dynamic Programming
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.