Program to find number of starting point from where we can start travelling in Python

Suppose there are n cities numbered from 0 to n-1 and there are n directed roads. We can travel from city i to city (i + 1) % n [0 to 1 to 2 to .... to N - 1 to 0]. We have a car with a fuel tank capacity of cap units. There are fuel[i] units of fuel available at city i and the car takes cost[i] units of fuel to travel from city i to (i + 1) % n. We need to find how many cities we can start from to travel around all cities and return to the starting point.

Problem Example

If the input is cap = 3, fuel = [3,1,2], costs = [2,2,2], then the output will be 2 because there are two possible starting points ?

  • Starting from city 0: Fill tank with 3 units, use 2 units to reach city 1. Tank has 1 unit left. Add 1 unit at city 1 (total 2 units), use 2 units to reach city 2. Tank is empty. Add 2 units at city 2, use 2 units to return to city 0.
  • Starting from city 2: Fill tank with 2 units, travel to city 0. Add 3 units at city 0 (total 3 units), use 2 units to reach city 1. Tank has 1 unit left. Add 1 unit at city 1 (total 2 units), use 2 units to return to city 2.

However, we cannot start from city 1 because there is only 1 unit of fuel available, but traveling to city 2 requires 2 units.

Algorithm Steps

To solve this problem, we follow these steps ?

  • n := size of fuel array
  • req := array of size n initialized with 0
  • For k in range 0 to 1, do:
    • For i from n-1 to 0 (reverse order), do:
      • nexti := (i + 1) mod n
      • req[i] := max(0, req[nexti] + costs[i] - fuel[i])
      • If min(req[i] + fuel[i], cap) - costs[i] < req[nexti], then return 0
  • Return count of zeros in req array

Implementation

def solve(cap, fuel, costs):
    n = len(fuel)
    req = [0] * n
    
    for k in range(2):
        for i in range(n-1, -1, -1):
            nexti = (i + 1) % n
            req[i] = max(0, req[nexti] + costs[i] - fuel[i])
            if min(req[i] + fuel[i], cap) - costs[i] < req[nexti]:
                return 0
    return sum(1 for r in req if r == 0)

cap = 3
fuel = [3, 1, 2]
costs = [2, 2, 2]
result = solve(cap, fuel, costs)
print(f"Number of valid starting points: {result}")
Number of valid starting points: 2

How It Works

The algorithm calculates the minimum fuel required to start from each city. The req[i] array stores the minimum fuel needed at city i to complete the journey. We iterate twice to ensure all dependencies are resolved. If any city requires more fuel than the tank capacity allows, we return 0 (impossible). Otherwise, we count cities where the required fuel is 0 (valid starting points).

Conclusion

This algorithm efficiently determines valid starting points for a circular journey by calculating minimum fuel requirements. The two-pass approach ensures accurate computation of dependencies between cities.

Updated on: 2026-03-26T18:14:07+05:30

554 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements