- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find minimum adjustment cost of an array in Python

Suppose we have an array of positive numbers; we replace each element from that array array so that the difference between two adjacent elements in the array is either less than or equal to a given target. Now, we have to minimize the adjustment cost, so the sum of differences between new value and old value. More precisely, we minimize ∑|A[i] – Anew[i]| where i in range 0 to n-1, here n is denoted as size of A and Anew is the array with adjacent difference less than or equal to target.

So, if the input is like [56, 78, 53, 62, 40, 7, 26, 61, 50, 48], target = 20, then the output will be 25

To solve this, we will follow these steps −

n := size of arr

table := [[0 for i in range 0 to M + 1] for i in range 0 to n]

for j in range 0 to M + 1, do

table[0, j] := |j - arr[0]|

for i in range 1 to n, do

for j in range 0 to M + 1, do

table[i, j] := 100000000

for k in range maximum of (j-target and 0) and minimum of (M and j + target), do

table[i,j] = minimum of table[i,j], table[i - 1,k] + |arr[i] - j|

ans := 10000000

for j in range 0 to M + 1, do

ans = minimum of ans and table[n-1, j]

return ans

## Example

Let us see the following implementation to get better understanding −

M = 100 def get_min_cost(arr, target): n = len(arr) table = [[0 for i in range(M + 1)] for i in range(n)] for j in range(M + 1): table[0][j] = abs(j - arr[0]) for i in range(1, n): for j in range(M + 1): table[i][j] = 100000000 for k in range(max(j - target, 0), min(M, j + target) + 1): table[i][j] = min(table[i][j], table[i - 1][k] + abs(arr[i] - j)) ans = 10000000 for j in range(M + 1): ans = min(ans, table[n - 1][j]) return ans arr= [56, 78, 53, 62, 40, 7, 26, 61, 50, 48] target = 20 print(get_min_cost(arr, target))

## Input

[56, 78, 53, 62, 40, 7, 26, 61, 50, 48], 20

## Output

35

- Related Articles
- Find minimum adjustment cost of an array in C++
- Program to find minimum cost for painting houses in Python
- Program to find minimum cost to merge stones in Python
- Program to find minimum cost to connect all points in Python
- Program to find minimum cost to cut a stick in Python
- Program to find minimum cost to hire k workers in Python
- Return the minimum of an array or minimum ignoring any NaNs in Python
- Return the minimum of an array along an axis or minimum ignoring any NaNs in Python
- Program to find minimum total cost for equalizing list elements in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum deletion cost to avoid repeating letters in Python
- Minimum Cost Tree From Leaf Values in Python
- Connecting Cities With Minimum Cost in Python\n
- Find indexes of multiple minimum value in an array in JavaScript
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python