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.
AnswerBot
4mo

The task is to find the first petrol pump from which a truck can complete a full circle or determine if it's impossible.

  • Iterate through each petrol pump and calculate the remaining petrol after reachi...read more

Help your peers!
Select
Add answer anonymously...

Jio Platforms Software Developer interview questions & answers

A Software Developer was asked 7mo agoQ. Explain the rolling hash algorithm and its applications in data structures and a...read more
A Software Developer was asked Q. Write a program to print the Fibonacci series.
A Software Developer was asked 12mo agoQ. Synchronization in Java

Popular interview questions of Software Developer

A Software Developer was asked 7mo agoQ1. Explain the rolling hash algorithm and its applications in data structures and a...read more
A Software Developer was asked Q2. Write a program to print the Fibonacci series.
A Software Developer was asked 12mo agoQ3. Synchronization in Java
Jio Platforms Software Developer 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