Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:53:52+00:00 2026-06-13T23:53:52+00:00

I appeared in an interview. I stuck in one question. I am asking the

  • 0

I appeared in an interview. I stuck in one question. I am asking the same.

Question: There is circular road is given. That road contains number
of petrol pumps. Each petrol pump have given amount of petrol.
Distance between each two consecutive petrol pump is also given. Now
there is a vehicle is given having empty fuel tank of limitless
capacity. Build an algorithm so that vehicle can cover complete round
without any backtracking. It is given that such path is definitely
possible.

Input: (int fuel[], int distance[])

Output: petrol pump index from where vehicle can make complete round of circular road.

My approaches:

  1. Check from each petrol pump, if fuel tank is empty in between path, move to next petrol pump. and start the same process again. This algorithm takes O(N^2), here N = number of petrol pumps.

  2. Then I move to the Binary search concept, to reduce the complexity to O(n*logn). But I failed to conclude the solution. I messed up in this solution.

  3. Then I try to apply some intelligence, by choosing that petrol pump whose left petrol is maximum in between that two petrol pumps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:53:55+00:00Added an answer on June 13, 2026 at 11:53 pm

    (This may be equivalent to the algorithm Evgeny Kluev posted, which I don’t understand. If so, that answer has priority.)

    Let F_i_j be the net, signed amount of fuel in the tank on arriving at j having started at i with zero fuel in the tank before filling at i.

    Calculate F_0_i for every node i by working around the circle adding fuel at each node and subtracting the fuel cost of each edge.

    If F_0_0, the net fuel at the end of a circuit starting at 0, is negative, then there is not enough fuel in the system (this is not supposed to happen according to the problem statement).

    If no F_0_i is negative, report 0 as result.

    Otherwise, find the node s with the most negative value of F_0_s. Pick s as the starting node.

    For any node i, F_s_i is equal to F_0_i + abs(F_0_s). Since F_0_s is the most negative F_0_i, that makes F_s_i non-negative.

    Worked example, as suggested in a comment by Handcraftsman:
    Label nodes 0 through 4
    
    node: 0,1,2,3,4
    fuel: 1,3,4,4,1
    dist: 1,4,2,2,2
    
    
    First calculate F_0_i for i = 1, 2, 3, 4, 0
    Start at node 0 with 0 fuel.
    
    f_0_1 = 0 + 1 - 1 = 0, min score 0, node 1;
    f_0_2 = 0 + 3 - 4 = -1, min score -1, node 2;
    f_0_3 = -1 + 4 - 2 = 1;
    f_0_4 = 1 + 4 - 2 = 3;
    f_0_0 = 3 + 1 - 2 = 2;
    
    f_0_0 is non-negative, so there is enough fuel.
    
    The minimum score was -1, at node 2, so the result is node 2.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While considering another problem one question appeared. I do not know how to write
I'm aware that this question has appeared in various forms before, but none of
(Please note that the behavior described in this question only appeared because of something
I appeared for php test, their I was asked one question for which I
In a previous question , it appeared that a plain return-by-value function always copies
I have these strange symbols that appeared one day, and I'm not quite sure
Okay, it appeared that I had an answer for this question, but I was
A strange error appeared in the error log in PHP, but the script that
I want to find the first element that has appeared in previous positions in
In addition to my previous question , another problem appeared and I decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.