Gas Station Tour Problem

You are presented with a circular track consisting of several petrol pumps. Each petrol pump is indexed from 0 to N-1 and offers certain attributes:

  • The quantity of petrol available at the pump.
  • The distance to the subsequent petrol pump.

Your objective is to determine the first petrol pump from which a truck, with an empty but unlimited fuel tank, can start its journey and complete the circle or establish that a full circle is not possible.

Note that the truck pauses at every pump, refuels, and consumes 1 litre of petrol per kilometre traveled.

Example:

Input:
3
4
4 6
6 5
7 3
4 5
3
6 4
5 3
8 6
2
2 3
2 2
Output:
1
0
-1

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 105
  • 1 ≤ Amount of petrol on each pump ≤ 109
  • 1 ≤ Distance to next pump ≤ 109

Note: You are to implement only the function as the output procedures are handled. Ensure that the indices in your results are 0-based.

AnswerBot
1mo

The Gas Station Tour Problem involves finding the first petrol pump from which a truck can start its journey and complete a circular track.

  • Calculate the difference between petrol available and distanc...read more

Help your peers!
Add answer anonymously...
Icertis Associate Software Engineer 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