The Circular Tour problem is a classic computer science problem, which involves finding a starting point in a circular route, from where one can complete the tour without running out of fuel.
def circular_tour(petrol, distance):
n = len(petrol)
start = 0
end = 1
current_petrol = petrol[start] - distance[start]
while start != end or current_petrol < 0:
while current_petrol < 0 and start != end:
current_petrol -= petrol[start] - distance[start]
start = (start + 1) % n
if start == 0:
return -1
current_petrol += petrol[end] - distance[end]
end = (end + 1) % n
return start
# Example usage:
petrol = [4, 6, 7, 4]
distance = [6, 5, 3, 5]
start = circular_tour(petrol, distance)
print("Starting point:", start) # Output: Starting point: 2