• Source
    1. import java.util.*;
    2.  
    3. /**
    4.  * Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data.
    5.  * 1. The amount of petrol that petrol pump will give.
    6.  * 2. Distance from that petrol pump to the next petrol pump.
    7.  *
    8.  * Calculate the first point from where a truck will be able to complete the circle
    9.  * (The truck will stop at each petrol pump and it has infinite capacity).
    10.  *
    11.  * Assume for 1 litre petrol, the truck can go 1 unit of distance.
    12.  *
    13.  * For example, let there be 4 petrol pumps with amount of petrol and distance to
    14.  * next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}.
    15.  * The first point from where truck can make a circular tour is 2nd petrol pump.
    16.  *
    17.  * Output should be “start = 1″ (index of 2nd petrol pump).
    18.  *
    19.  */
    20. class Ideone {
    21.  
    22. public static void main(String[] args){
    23. //ith element in petrol represents petrol available at pump i and distances[i] represents distance from i to i+1 petrol pump
    24. int[] petrol = {4,6,5,4};
    25. int[] distances = {6,5,3,5};
    26.  
    27. //Keep two pointers to track start and end points of traversal path
    28. //Also move in one direction. Once you found you cannot move forward with existing petrol, extend in other direction
    29. //When starting end end pointers are equal, you have completed the circular path
    30. int startIndex = 0;
    31. int endIndex = 0;//Max Index + 1
    32. int leftOverPetrol = 0;
    33.  
    34. while(true){
    35. int totalPetrolAvailable = leftOverPetrol + petrol[endIndex];
    36. if(distances[endIndex] > totalPetrolAvailable){
    37. // Doesn't have any fuel to move forward. So include last point(move backward)
    38. if(startIndex == 0){
    39. startIndex = petrol.length - 1;//Set pointer to last petrol pump
    40. }else{
    41. startIndex--; //Include previous pump to start index
    42. }
    43. leftOverPetrol = leftOverPetrol + petrol[startIndex] - distances[startIndex];
    44. }else{
    45. leftOverPetrol = totalPetrolAvailable - distances[endIndex];
    46. if(endIndex == petrol.length -1){
    47. endIndex = 0;
    48. }else{
    49. endIndex++;
    50. }
    51. }
    52.  
    53. if(startIndex == endIndex) break; //Made a circular loop
    54. }
    55.  
    56. if(leftOverPetrol >= 0){
    57. System.out.println("Start index to complete loop => "+ startIndex);
    58. }else{
    59. System.out.println("Circular loop not possible");
    60. }
    61. }
    62.  
    63. //Approach 2 : We can create 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.
    64. }
    65.