#K96157. Kangaroo Rock Hopping
Kangaroo Rock Hopping
Kangaroo Rock Hopping
You are given n rocks in a row, numbered from 1 to n, with each rock having a certain height. A kangaroo starts at the first rock and wants to reach the last rock by jumping forward. However, the kangaroo is only able to jump from rock i to rock j (with j > i) if the absolute difference in heights does not exceed k.
Formally, the kangaroo can jump from rock i to rock j if:
\(|heights[j] - heights[i]| \leq k\)
Your task is to determine the minimum number of jumps required for the kangaroo to reach the last rock, or print -1
if it is not possible.
Note: The input is provided via stdin
and the output should be printed to stdout
.
inputFormat
The first line contains two integers n
and k
separated by a space, where n
is the number of rocks and k
is the maximum allowed height difference the kangaroo can jump.
The second line contains n
integers representing the heights of the rocks from the first to the last.
Constraints:
- 2 ≤ n ≤ 105
- 0 ≤ heights[i] ≤ 109
- 0 ≤ k ≤ 109
outputFormat
Output a single integer representing the minimum number of jumps required for the kangaroo to reach the last rock. If it is not possible to reach the last rock, output -1
.
5 3
1 2 2 5 2
2
</p>