Asked inDeutsche Bank,Technical Analyst-Intern

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.

Be the first one to answer
Add answer anonymously...
Deutsche Bank Technical Analyst-Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter