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.
The cumulative array of petrol should be > cumulative array of distance for every element
distance array 1 3 2 1 3 5
petrol array 1 2 4 3 3 2
If we start at P1,
Cumulative array for distance = 1 4 6 7 10 15
Cumulative array for petrol = 1 3 7 10 13 15
If we start at P3,
Cumulative array for distance = 2 3 6 11 12 15
Cumulative array for petrol = 4 7 10 12 13 15
So P3 is our answer
Create a cumulative array starting from P1. Traverse the array and find the point (Px), where all elements to right of that
have property such that Pi > Di for all i > x AND
Pj > Dj for all j < x
Px+1 is your answer
otherwise not possible.
One example of not possible is
Distance array 2 8 5 4 1 6 2 2
Petrol array 3 5 7 3 5 3 2 2
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks