Minimum Stops for Ninja's Journey Problem Statement

Ninja wishes to travel from his house to a destination X miles away. He knows there are Y gas stations along the way, each station i located at a distance d[i] from the house with g[i] liters of gas available. His vehicle requires 1 liter of gas per mile.

Ninja starts with Z liters of fuel in his tank, which has infinite capacity. He can refuel his vehicle completely at any station if necessary.

Determine the minimum number of refueling stops Ninja must make to reach his destination.

Note:

If Ninja arrives at a station or the destination with no fuel, he can still refuel or be considered to have arrived.

Input:

The first line contains an integer T for the number of test cases to process.
Each test case has:
- A line with three integers: X (distance to destination), Y (number of stations), Z (starting fuel).
- Y pairs, each pair containing d[i] (distance to station) and g[i] (gas available at station).

Output:

For each test case, output a single integer, representing the minimum number of stops needed to reach the destination. Print each result on a new line.

Example:

Input:
X = 10, Y = 4, Stations = [[1, 6], [2, 3], [3, 3], [6, 4]], Z = 1
Output:
2
Explanation:

Starting with 1L, move to position 1: use 1L, refuel 6L. Move to position 6: use 5L, refuel 4L. Move to destination: use 4L. Total stops: 2.

Constraints:

  • 1 <= T <= 10
  • 1 <= X, Z <= 107
  • 0 <= Y <= 105
  • 1 <= d[i], g[i] <= 107
  • Time limit: 1 second
AnswerBot
4mo

Calculate the minimum number of refueling stops needed for a ninja to reach a destination with given gas stations and starting fuel.

  • Iterate through gas stations while maintaining current fuel level an...read more

Raja
3mo
function minRefuelStops(X, Y, Z, stations) { stations.push([X, 0]); // Add destination as a station with 0 gas stations.sort((a, b) => a[0] - b[0]); let maxHeap = []; let fuel = Z; let i = 0; let stop...read more
Help your peers!
Select
Add answer anonymously...

Cognizant Trainee Analyst interview questions & answers

A Trainee Analyst was asked 2mo agoQ. What is the highest background
A Trainee Analyst was asked Q. Minimum Stops for Ninja's Journey Problem Statement Ninja wishes to travel from ...read more
A Trainee Analyst was asked Q. Black Friday Discount Challenge Imagine it's Black Friday and a supermarket is o...read more

Popular interview questions of Trainee Analyst

A Trainee Analyst was asked 2mo agoQ1. What is the highest background
A Trainee Analyst was asked Q2. Minimum Stops for Ninja's Journey Problem Statement Ninja wishes to travel from ...read more
A Trainee Analyst was asked Q3. Black Friday Discount Challenge Imagine it's Black Friday and a supermarket is o...read more
Cognizant Trainee Analyst 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