# Find the first circular tour that visits all petrol pumps

You are given an array of petrol pumps, where each element of the array represents a petrol pump. Each petrol pump has two attributes:

Petrol: The amount of petrol that the pump can provide.

Distance: The distance to the next petrol pump.

The task is to find a petrol pump from where a circular tour can be started and completed so you can travel through each petrol pump and return to the starting point. Each petrol pump provides a certain amount of petrol, and the distance between consecutive petrol pumps consumes some petrol.

Let us implement the code for it:

CODE:

Output:

Explanation:

Struct Definition:

The code defines a PetrolPump structure with two attributes: petrol and distance. Each element in the array represents a petrol pump with these attributes.

Function findStartingPoint:

This function takes an array of petrol pumps (arr) and the number of pumps (n) as input parameters.

It implements the algorithm to find the starting point for a circular tour.

Initialization:

Start and end are initialized to 0 and 1, respectively, indicating the current starting and ending petrol pumps in the tour.

curr_petrol is initialized with the difference between the petrol and the distance of the starting petrol pump.

Main Loop:

The code uses a while loop that continues until a suitable starting point is found (when end equals start and curr_petrol is non-negative).

Check and Update Current Petrol:

If curr_petrol becomes negative, it means the current starting point is not suitable.

The code enters a nested while loop to find a new starting point (start).

It subtracts the petrol used to travel from the removed petrol pump to the next one, and increments start.

This loop continues until a valid starting point is found or all pumps are checked.

Move to the Next Petrol Pump:

The code updates end to the next petrol pump circularly.

Update Current Petrol:

curr_petrol is updated by adding the petrol provided by the current pump and subtracting the distance to the next pump.

Return Starting Point:

If a suitable starting point is found, the function returns the index of that pump as the starting point.

If no suitable starting point is found after iterating through all petrol pumps, it returns -1.

Main Function:

The main function is to create an array of petrol pumps (arr) for demonstration purposes.

The findStartingPoint function is called with this array, and the result is stored in the variable start.

Output:

If a starting point is found (start != -1), it prints the index of the petrol pump from where the circular tour can be started.

If no solution exists (start == -1), it prints a message indicating that no suitable starting point was found.

Let us discuss the time and space complexity:

Time Complexity:

The time complexity of the code is O(N), where N is the number of petrol pumps.

Explanation:

The main loop runs until a suitable starting point is found, iterating through each petrol pump at most twice (once for start and once for end).

Each loop iteration involves constant time operations (arithmetic, comparisons, and updates).

The nested while loop, in the worst case, iterates through all petrol pumps once, contributing O(N) to the time complexity.

Therefore, the overall time complexity is O(N).

Space Complexity:

The space complexity of the code is O(1), constant space.

Explanation: The code uses a constant amount of extra space regardless of the input size.

The only variables used are start, end, curr_petrol, and the loop control variables.

No additional data structures or arrays that depend on the input size are created.

Therefore, the overall space complexity is O(1).