We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

This goes into an infinite loop. What happens is-for a series of 0 to 6 here, it exhausts first at the node 4, starts off again at 4, and then after exhauting at node 6, arrives at 0 again, and the cycle repeats. We can use a hashmap mayB to mark off the ones used as start node. Just thinking out aloud.

The problem did not specify what to ouput for an invald input, but we can output -1 or something.

All test cases have a solution, i.e., there is no case where there's no solution. We have to make sure that our test case has a solution. The mentioned test case clearly has no solution.

Yeah it is because you are never going to reach the starting point no matter from where you start from. If you check properly you total fuel is more than the total distance to be covered where mileage is 1km per litre

That should be "total fuel is LESS than the distance to be covered" so there's no solution. The problem guarantees a solution for given data so this data (total fuel = 19, total distance = 25) does not qualify. There is always a solution if (total fuel >= total distance) but no solution if (total fuel < total distance).

## Truck Tour

You are viewing a single comment's thread. Return to all comments →

Thanks man. It passed all the given test cases. But this one(not given in hackerrank) failed:

7

5 2

1 2

3 3

2 5

6 2

1 5

1 6

Or m I missing something?

Sorry, I have no access to my laptop to check it. What is result of my code on this input, and what is the answer/sequence you expect for fhis input?

This goes into an infinite loop. What happens is-for a series of 0 to 6 here, it exhausts first at the node 4, starts off again at 4, and then after exhauting at node 6, arrives at 0 again, and the cycle repeats. We can use a hashmap mayB to mark off the ones used as start node. Just thinking out aloud.

The problem did not specify what to ouput for an invald input, but we can output -1 or something.

I believe there is always a solution.

All test cases have a solution, i.e., there is no case where there's no solution. We have to make sure that our test case has a solution. The mentioned test case clearly has no solution.

Yeah it is because you are never going to reach the starting point no matter from where you start from. If you check properly you total fuel is more than the total distance to be covered where mileage is 1km per litre

That should be "total fuel is LESS than the distance to be covered" so there's no solution. The problem guarantees a solution for given data so this data (total fuel = 19, total distance = 25) does not qualify. There is always a solution if (total fuel >= total distance) but no solution if (total fuel < total distance).

Total Sum At All Petrol Pumps Petrol = 19 Ditance = 25 > 19

So it will go into infinite loop. Because Total Distance Itself is not withing the total capacity Of all the petrol pumps.

It was a simple check mam...!!