Difficulty Level:


Total Submissions: 76 Accuracy:


Find minimum adjustment cost of an array

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.


The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains two integers N and K where N denotes the size of array A and K is the target.

The second line of each test case contains N space separated integers denoting elements of the array A[ ].


Print the minimum adjustment cost for each test case in a new line.


1<= T <=100

1<= N, K <=1000

1<= A[ ] <=100



4 1

1 3 0 3



** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sujnesh Mishra

Set Default Code

It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use, generate link and share the link here.