PTRLP Editorial

Problem Explanation

You are given two arrays Petrol and cost. For visiting each index you gain Petrol_i amount of petrol and lose cost_i amount of petrol. Given that the array is a circular array(the next index from the last index is the first index), you have to tell from which index can Alice visit all the indices or tell if it is impossible to do so.

Approach

If the Total cost is more than the Total Petrol, then we can never visit all the stations. Otherwise we can iterate from the beginning of the arrays and add the petrol gained at each station which subtracting the cost of reaching each station from the current petrol. If the current petrol is less than 0 then we can never start from the decided index, so we reset the current petrol to 0 and set the starting index to i+1.