Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data.

1. The amount of petrol that every petrol pump has.
2. Distance from that petrol pump to the next petrol pump.

Calculate the first point from where a truck will be able to complete the circle (The truck will stop at each petrol pump and it has infinite capacity). Expected time complexity is O(n). Assume for 1 litre petrol, the truck can go 1 unit of distance.

For example, let there be 4 petrol pumps with amount of petrol and distance to next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}. The first point from where truck can make a circular tour is 2nd petrol pump. Output should be “start = 1” (index of 2nd petrol pump).

[ad type=”banner”]

A Simple Solution is to consider every petrol pumps as starting point and see if there is a possible tour. If we find a starting point with feasible solution, we return that starting point. The worst case time complexity of this solution is O(n^2).

We can use a Queue to store the current tour. We first enqueue first petrol pump to the queue, we keep enqueueing petrol pumps till we either complete the tour, or current amount of petrol becomes negative. If the amount becomes negative, then we keep dequeueing petrol pumps till the current amount becomes positive or queue becomes empty.

Instead of creating a separate queue, we use the given array itself as queue. We maintain two index variables start and end that represent rear and front of queue.

Python Programming

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20find%20circular%20tour%20for%20a%20track%20%0A%20%0A%23%20A%20petrol%20pump%20has%20petrol%20and%20distance%20to%20next%20petrol%20pimp%0Aclass%20PetrolPump%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2Cpetrol%2C%20distance)%3A%0A%20%20%20%20%20%20%20%20self.petrol%20%3D%20petrol%0A%20%20%20%20%20%20%20%20self.distance%20%3D%20distance%20%0A%20%0A%23%20The%20funtion%20return%20starting%20point%20if%20there%20is%20a%20possible%0A%23%20solution%20otherwise%20returns%20-1%20%0Adef%20printTour(arr)%3A%0A%20%20%20%20%20%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%20%20%20%23%20Consider%20first%20petrol%20pump%20as%20starting%20point%0A%20%20%20%20start%20%3D%200%0A%20%20%20%20end%20%3D%201%0A%20%20%20%20%20%0A%20%20%20%20curr_petrol%20%3D%20arr%5Bstart%5D.petrol%20-%20arr%5Bstart%5D.distance%20%0A%20%20%20%20%20%0A%20%20%20%20%23%20Run%20a%20loop%20whie%20all%20petrol%20pumps%20are%20not%20visited%0A%20%20%20%20%23%20And%20we%20have%20reached%20first%20petrol%20pump%20again%20with%200%20%0A%20%20%20%20%23%20or%20more%20petrol%0A%20%20%20%20while(end%20!%3D%20start%20or%20curr_petrol%20%3C%200%20)%3A%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20If%20current%20amount%20of%20petrol%20pumps%20are%20not%20visited%0A%20%20%20%20%20%20%20%20%23%20And%20we%20have%20reached%20first%20petrol%20pump%20again%20with%0A%20%20%20%20%20%20%20%20%23%200%20or%20more%20petrol%20%0A%20%20%20%20%20%20%20%20while(curr_petrol%20%3C%200%20and%20start%20!%3D%20end)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Remove%20starting%20petrol%20pump.%20Change%20start%0A%20%20%20%20%20%20%20%20%20%20%20%20curr_petrol%20-%3D%20arr%5Bstart%5D.petrol%20-%20arr%5Bstart%5D.distance%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20(start%20%2B1)%25n%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20If%200%20is%20being%20considered%20as%20start%20again%2C%20then%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20there%20is%20no%20possible%20solution%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20start%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%0A%20%0A%20%20%20%20%20%20%20%20%23%20Add%20a%20petrol%20pump%20to%20current%20tour%0A%20%20%20%20%20%20%20%20curr_petrol%20%2B%3D%20arr%5Bend%5D.petrol%20-%20arr%5Bend%5D.distance%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20end%20%3D%20(end%20%2B1)%20%25%20n%0A%20%0A%20%20%20%20return%20start%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aarr%20%3D%20%5BPetrolPump(6%2C4)%2C%20PetrolPump(3%2C6)%2C%20PetrolPump(7%2C3)%5D%0Astart%20%3D%20printTour(arr)%0A%20%0Aprint%20%22No%20solution%22%20if%20start%20%3D%3D%20-1%20else%20%22start%20%3D%22%2C%20start%0A%20%0A%0A” message=”” highlight=”” provider=”manual”/]

Output:

start = 2

Time Complexity: Seems to be more than linear at first look. If we consider the items between start and end as part of a circular queue, we can observe that every item is enqueued at most two times to the queue. The total number of operations is proportional to total number of enqueue operations. Therefore the time complexity is O(n).

[ad type=”banner”]

Categorized in: