Circular tour | Practice | GeeksforGeeks

Suppose there is a circle. There are N petrol pumps on that circle. You will be 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.
Find a starting point where the truck can start to get through the complete circle without exhausting its petrol in between.
Note :  Assume for 1 litre petrol, the truck can go 1 unit of distance.
Example 1:
Input:
N = 4
Petrol = 4 6 7 4
Distance = 6 5 3 5
Output: 1
Explanation: There are 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 in this case is 1
(index of 2nd petrol pump).

Your Task:
Your task is to complete the function tour() which takes the required data as inputs and returns an integer denoting a 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). If there exists multiple such starting points, then the function must return the first one out of those. (return -1 otherwise)
Expected Time Complexity: O(N)
Expected Auxiliary Space : O(1)
Constraints:
2 ≤ N ≤ 10000
1 ≤ petrol, distance ≤ 1000
 
0
chintubhuarya752 days ago
int sum=0, point=0;       int i;       for( i=0; i<n; i++) {           sum+=p[i].petrol-p[i].distance;           if(sum<0) {               point=i+1;               sum=0;           }
       }              int j=0;       while(point<=n && j<point && j<n) {           sum+=p[j].petrol-p[j].distance;           j++;                 }       if(sum<0) return -1;       return point;
 
0
akashchourasiya84 days ago
it is same as KEYDAN ALGO
+1
sabaperween92 weeks ago
TC=O(n)
SC=O(1)
 
 
    int tour(int petrol[], int distance[])   {    int n=petrol.length;    int start=0;    int end=1;    int count=petrol[start]-distance[start];    while(start!=end||count<0){        while(count<0&&start!=end){            count-=petrol[start]-distance[start];            start=(start+1)%n;            if(start==0)                return -1;        }        count+=petrol[end]-distance[end];        end=(end+1)%n;    }    return start;       }
0
rajneesh02012 weeks ago
int tour(petrolPump p[],int n)   {      //Your code here      int start=0;      int end=1;            int curr_pet=p[start].petrol - p[start].distance;      while(end!=start || curr_pet<0)      {          while(curr_pet<0 and start !=end)          {              curr_pet -=p[start].petrol-p[start].distance;              start=(start+1)%n;                            if(start==0)               return -1;          }          curr_pet +=p[end].petrol-p[end].distance;          end=(end+1)%n;                }      return start;   }
0
masumasif112 weeks ago
896 25 46 83 68 15 65 35 51 44 9 88 79 77 85 89
 
for this input expected output is wrong
+1
bhaskarmaheshwari83 weeks ago
int remaining=0;      int required=0;      int start=0;      for(int i=0;i<n;i++)      {          remaining=p[i].petrol-p[i].distance+remaining;          if(remaining<0)          {              required+=remaining;              start=i+1;              remaining=0;          }      }      if(remaining+required>=0)      return start;      return -1;
+1
bhaskarmaheshwari83 weeks ago
O(n^2) 
 
 for(int i=0;i<n;i++)      {          int cnt=0;          int start=i;          int remaining=0;          for(int j=i;j<n;j++)          {              if((p[j].petrol-p[j].distance+remaining)<0)               break;               remaining=(p[j].petrol-p[j].distance+remaining);               cnt++;               if(j==n-1&&cnt<n)               {                   j=-1;               }               if(cnt==n)               return start;          }      }            return -1;
0
bhaskarmaheshwari8
This comment was deleted.
+1
mishra07adi3 weeks ago
C++ Sol
class Solution{
  public:
  
    //Function to find starting point where the truck can start to get through
    //the complete circle without exhausting its petrol in between.
    int tour(petrolPump p[],int n)
    {
       //Your code here
       int deficit = 0;
       int balance = 0;
       int start = 0;
       for(int i=0;i<n;i++){
           balance += p[i].petrol - p[i].distance;
           if(balance < 0){
               deficit+=balance;
               start = i+1;
               balance = 0;
           }
       }
       if(deficit + balance >= 0)
           return start;
        else 
           return -1;
    }
};
0
ram9999mishra3 weeks ago
Simple Java solution with 100 % case pass
class Solution
{
    //Function to find starting point where the truck can start to get through
    //the complete circle without exhausting its petrol in between.
    int tour(int petrol[], int distance[])
    { 
       int n = petrol.length;
       int deficit = 0;
       int balance = 0;
       int start = 0;
       for(int i=0;i<n;i++){
           balance += petrol[i] - distance[i];
           if(balance < 0){
               deficit+=balance;
               start = i+1;
               balance = 0;
           }
       }
       if(deficit + balance >= 0)
           return start;
        else 
           return -1;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.