Minimum Cost to Reach End Problem

You are provided with an array ARR of integers of size 'N' and an integer 'K'. The goal is to move from the starting index to the end of the array with the minimum possible cost, considering you can jump from index i to j if j ≤ i + K. The cost to jump from index i to index j is given by abs(ARR[j] - ARR[i]).

Example:

Input:
ARR = [10, 3, 40, 5, 25]
K = 2
Output:
29
Explanation:

The optimal jumps are: from index 0 to 1 (cost: 7), then from index 1 to 3 (cost: 2), and finally from index 3 to 4 (cost: 20). Total cost = 7 + 2 + 20 = 29.

Constraints:

  • 1 ≤ T ≤ 10
  • 2 ≤ N ≤ 1000
  • 1 ≤ K < N
  • 0 ≤ ARR[i] <= 10^6
  • Time Limit: 1 sec
AnswerBot
4mo

Find minimum cost to reach end of array by jumping with constraints

  • Use dynamic programming to keep track of minimum cost at each index

  • Iterate through the array and update the minimum cost based on rea...read more

Help your peers!
Select
Add answer anonymously...

Visa Software Developer interview questions & answers

A Software Developer was asked Q. How would you design Google Pay?
A Software Developer was asked Q. HLD of recursive
A Software Developer was asked Q. Why did you only pass 10 out of 14 test cases in the 4th question?

Popular interview questions of Software Developer

A Software Developer was asked Q1. How would you design Google Pay?
A Software Developer was asked Q2. HLD of recursive
A Software Developer was asked Q3. Why did you only pass 10 out of 14 test cases in the 4th question?

Top HR questions asked in Visa Software Developer

A Software Developer was asked Q1. Tell me about your project written in [programming language].
A Software Developer was asked Q2. Could you please explain your resume?
A Software Developer was asked Q3. Why do you want to apply for a VISA?
Visa Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits