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.

I'm also confused why this is under the queue section. I solved it without a queue, stack, list, etc. Just keep track of how much fuel is in the tank. Once it goes below zero then reset the fuel in the tank to zero and set the pump to the next pump.

According to your algorithm if we supply the following output
4
2 1
1 4
2 2
1 1

it gives the output as 2 wheras technically there is no solution to this scenario as you are only considering from the point until the end of the list whereas it should be considered as a queue which wraps around. Please correct me if I am wrong.

It was a while ago so I can't say for sure what my assumptions were. If I had to guess I would say I was under the assumption a solution must exist for the data supplied and thus my algorithm works correctly under that assumption. At least I think it does. Do you have an example where it doesn't?

Since no where it is asked to return -1 if solution does not exists, your assumption is correct. A solution must exists. Hence, we do not need to use a circular queue. :)

## Truck Tour

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

I'm also confused why this is under the queue section. I solved it without a queue, stack, list, etc. Just keep track of how much fuel is in the tank. Once it goes below zero then reset the fuel in the tank to zero and set the pump to the next pump.

According to your algorithm if we supply the following output 4 2 1 1 4 2 2 1 1

it gives the output as 2 wheras technically there is no solution to this scenario as you are only considering from the point until the end of the list whereas it should be considered as a queue which wraps around. Please correct me if I am wrong.

It was a while ago so I can't say for sure what my assumptions were. If I had to guess I would say I was under the assumption a solution must exist for the data supplied and thus my algorithm works correctly under that assumption. At least I think it does. Do you have an example where it doesn't?

Since no where it is asked to return -1 if solution does not exists, your assumption is correct. A solution must exists. Hence, we do not need to use a circular queue. :)

same doubt

The problem does not require Queue structre to use but the logic/understanding of the queue helps in breaking down the solution.

You are right :)

According to u passing input 3 1 25 2 1 3 1 answer would be 1,but technically answer should be not possible

1

5 6

3 1

2 4

you will get a wrong output for this case the ans to this input should be 0(1st petrol pump) but your code will give 1(2nd petrol pump).