Minimum Cost to Reach the End Problem Statement
Given an array of integers ARR
and an integer 'K', your goal is to reach the end of the array starting from the beginning with minimum cost. You can move from any index 'i' to index 'j' if j ≤ i + K
. The cost of movement from index 'i' to 'j' is abs(ARR[j] - ARR[i])
. Find the minimum cost to reach the end of the array when a maximum jump of 'K' is allowed.
Input:
For each test case:
The first line contains an integer T
specifying the number of test cases.
For each test case, the first line contains two space-separated integers, N
and K
.
The second line contains N
space-separated integers representing the elements of the array ARR
.
Output:
For each test case, output the minimum cost as a single integer on a new line.
Example:
Suppose you have an array ARR = [10, 3, 40, 5, 25]
and K = 2
. The minimum cost is 29. Explanation: Jump from index 0 to 1 with cost 7, from index 1 to 3 with cost 2, and from index 3 to 4 with cost 20. The total is 29.
Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 1000
1 ≤ K < N
0 ≤ ARR[i] ≤ 106
- Time Limit: 1 second.
Note:
You do not need to print anything; just implement the given function to return the answer.
Top Deutsche Bank Technical Analyst-Intern interview questions & answers
Top HR questions asked in Deutsche Bank Technical Analyst-Intern
Reviews
Interviews
Salaries
Users/Month