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 3986304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:59:44+00:00 2026-05-20T05:59:44+00:00

There are n petrol bunks arranged in circle. Each bunk is separated from the

  • 0

There are n petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1litre of petrol to cover 1km distance. You can’t infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of litres of petrol in all the bunks is equal to the distance to be covered.

ie let P1, P2, … Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel.

  • 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-05-20T05:59:45+00:00Added an answer on May 20, 2026 at 5:59 am

    Let’s choose a junk algorithm that we know is wrong to see why it is wrong…

    Notation…

    Current Point: (gallons of gas at Current Point, gallons required to make next point)-> Remaining Gas (gallons)

    In a little more mathematical form:

    P[i]: (g(P[i]), d(P[i+1])) -> sum of (g(P[i]) – d(P[i+1])) from i=1 to current point-1

    (And now for the bad algorithm…)

    P1: (2,2) -> 0 (at P2)
    P2: (5,3) -> 2 (at P3)
    P3: (2,4) -> 0 (at P4)
    P4: (2,5) -> -3 (ran out of gas 3 miles short of P5)
    

    In order to make it to P5, we have to have three extra gallons of gas by the time we make it to P3, and in order to have 3 extra gallons at P3, we need to have 3 extra gallons at P1:

    ??? -> +3 (at P1)
    P1: (2,2) -> 0+3 = 3 (at P2)
    P2: (5,3) -> 2+3 = 5 (at P3)
    P3: (2,4) -> 0+3 = 3 (at P4)
    P4: (2,5) -> -3 +3 = 0 (made it to P5)
    

    The trick, therefore, is to find the very worst sections — where you are not given enough gas to traverse them. We know we can’t start from P4, P3, P2, or P1. We have to start somewhere earlier and save up enough gas to make it through the bad section.

    There will no doubt be multiple bad sections within the circle, making this somewhat complicated, but it’s actually quite easy to figure out how to do this.

    It’s possible that the next few points following the very worst stretch in the circle could be traveled after the stretch, but only if they make no changes to your gas reserves. (e.g. the point after the worst stretch gives you 2 gallons of gas and makes you go 2 gallons of distance to the next point.)

    In some cases, however, the worst section MUST be covered last. That’s because before you start on that section, you need as much gas saved up as possible, and the next point after the worst stretch might give you the very last bit of gas that you need, which means you need to traverse it prior to taking on the worst stretch. Although there may exist multiple solutions, the simple fact of the matter is that traversing the worst section last is ALWAYS a solution. Here’s some code:

    class point_ {
    int gasGiven_;
    int distanceToNextPoint_;
    
    public:
    int gasGiven() {return gasGiven_;}
    int distanceToNextPoint {return distanceToNextPoint_;}
    }
    
    class Circle_ {
    public:
    numberOfPoints;
    point_ *P;
    }
    

    In main():

    int indexWorstSection=0;
    int numberPointsWorstSection=0;
    int worstSum=0;
    int currentSum=0;
    int i=0; 
    int startingPoint =0;
    
    // construct the circle, set *P to malloc of numberOfPoints point_'s, fill in all data
    
    while (i<(Circle.numberOfPoints-1) || currentSum<0)
       {
       currentSum += Circle.P[i].gasGiven() - Circle.P[i].distanceToNextPoint();
    
       if (currentSum < worstSum) { worstSum = currentSum; indexWorstSection=i-numberPointsWorstSection; startingPoint=i;}
    
       if (currentSum>0) { currentSum=0; } 
         else { numberPointsWorstSection++; }
    
       if (i==(Circle.numberOfPoints-1)) { i=0; }
         else { i++; }
       }
    
    if (indexWorstSection<0) indexWorstSection=Circle.numberOfPoints+indexWorstSection;
    

    The reason why you can’t make it a for-loop is because the worst section might be, for example, from i=(Circle.numberOfPoints -2) to i=3. If the currentSum is under zero, it must continue back at the start of the array.

    Haven’t tried the code and haven’t done any serious programming in almost a decade. Sorry if it has some bugs. You will no doubt have to clean this up a bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are several types of objects in a system, and each has it's own
There are 3 different ways to get data out of a BLOB column from
There are many tutorials that talk about deleting index.php from the url. But I
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking
There seem to be many ways to define singletons in Python. Is there a
There are a few ways to get class-like behavior in javascript, the most common
There are numerous Agile software development methods. Which ones have you used in practice

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.