#C6776. Minimizing Adjustments for Adjacent Height Differences
Minimizing Adjustments for Adjacent Height Differences
Minimizing Adjustments for Adjacent Height Differences
Given n buildings with heights represented by an array \(a_1, a_2, \dots, a_n\), and an integer \(k\), your task is to determine the minimum number of adjustments required so that the absolute difference between the heights of any two consecutive buildings does not exceed \(k\). In other words, for every \(i\) (\(2 \le i \le n\)), you need \(|a_i - a_{i-1}| \le k\).
If \(|a_i - a_{i-1}| > k\), you are allowed to adjust the height of the \(i^{th}\) building to:
- \(a_{i-1} + k\) if \(a_i > a_{i-1}\), or
- \(a_{i-1} - k\) if \(a_i < a_{i-1}\).
Your goal is to achieve the desired property with the minimum number of adjustments. It is guaranteed that using this method, the problem always has a solution.
inputFormat
The first line contains two integers \(n\) and \(k\) (the number of buildings and the maximum allowed difference, respectively).
The second line contains \(n\) space-separated integers representing the heights of the buildings.
outputFormat
Output a single integer, which is the minimum number of adjustments required so that for every two adjacent buildings \(a_{i-1}\) and \(a_i\), \(|a_i - a_{i-1}| \le k\).
## sample5 3
1 9 3 10 4
2