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:
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.
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.
Then I try to apply some intelligence, by choosing that petrol pump whose left petrol is maximum in between that two petrol pumps.