Circular Tour Problem Statement

Consider a circular path with N petrol pumps. Each pump is numbered from 0 to N-1. Every petrol pump provides:

  1. The amount of petrol available at the pump.
  2. The distance to the next petrol pump.

A truck with an empty tank and infinite capacity needs to complete a tour starting from one of these petrol pumps. The task is to find the first petrol pump from which the truck can complete the full circle or to determine that it's impossible.

The truck stops at every petrol pump to refill its tank and moves one kilometer for each litre of petrol.

Input:

The first line contains a single integer N, the number of petrol pumps.
Each of the following N lines contains two space-separated integers: the amount of petrol at the pump and the distance to the next pump.

Output:

Print the index of the first petrol pump from which the tour can be completed. If it's not possible, print -1.
Note: Use 0-based indexing.

Example:

Input:
4
4 6
6 5
7 3
4 5
Output:
1

Constraints:

  • 1 <= N <= 5*105
  • 1 <= Amount of petrol on each pump <= 109
  • 1 <= Distance to next pump <= 109
  • Time Limit: 1 sec

Note:

Implement the required function; no need to handle input/output directly as it's taken care of externally.
Be the first one to answer
Add answer anonymously...
Jio Platforms Software Developer 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