Find the maximum distance covered using n bikes in Python

Suppose there are n bikes and each can cover 100 km when they are fully fueled. We have to find the maximum amount of distance we can go using these n bikes. Here we can assume that all bikes are similar and a bike consumes 1 litre of fuel to cover 1 km distance.

The key insight is that instead of running all bikes in parallel (which only covers 100 km), we can transfer fuel strategically. When bikes run serially and transfer fuel at optimal points, we can cover much more distance without overflowing fuel tanks.

Problem Analysis

The optimal strategy is to transfer fuel from the last bike to other bikes at calculated intervals. Each bike drops out after transferring its remaining fuel, allowing the remaining bikes to continue further.

Optimal Fuel Transfer Strategy Bike 1: Goes full distance Bike 2: Transfers fuel Bike 3: Transfers fuel Start 33.33 km 83.33 km 183.33 km Transfer 1 Transfer 2

Algorithm

The algorithm works by calculating the distance each bike can contribute before transferring fuel to the remaining bikes ?

def maximum_distance(n, fuel):
    covered_distance = 0
    while n > 0:
        covered_distance = covered_distance + (fuel / n)
        n = n - 1
    return covered_distance

n = 3
fuel = 100
result = maximum_distance(n, fuel)
print(f"Maximum distance with {n} bikes: {result:.2f} km")
Maximum distance with 3 bikes: 183.33 km

How It Works

The mathematical reasoning behind this algorithm:

  • With n bikes, each travels fuel/n distance before the last bike transfers its remaining fuel

  • This ensures optimal fuel distribution without tank overflow

  • The process continues with n-1 bikes until only one bike remains

Step-by-Step Example

def maximum_distance_detailed(n, fuel):
    covered_distance = 0
    original_n = n
    
    print(f"Starting with {n} bikes, each with {fuel} units of fuel\n")
    
    while n > 0:
        segment_distance = fuel / n
        covered_distance += segment_distance
        print(f"Step {original_n - n + 1}: {n} bikes travel {segment_distance:.2f} km")
        print(f"Total distance so far: {covered_distance:.2f} km")
        n = n - 1
        if n > 0:
            print(f"Bike {n + 1} transfers fuel to remaining {n} bikes\n")
        else:
            print("\nJourney complete!")
    
    return covered_distance

result = maximum_distance_detailed(3, 100)
Starting with 3 bikes, each with 100 units of fuel

Step 1: 3 bikes travel 33.33 km
Total distance so far: 33.33 km
Bike 3 transfers fuel to remaining 2 bikes

Step 2: 2 bikes travel 50.00 km
Total distance so far: 83.33 km
Bike 2 transfers fuel to remaining 1 bikes

Step 3: 1 bikes travel 100.00 km
Total distance so far: 183.33 km

Journey complete!

Mathematical Formula

The maximum distance can be calculated using the harmonic series ?

def maximum_distance_formula(n, fuel):
    """Calculate maximum distance using harmonic series formula"""
    harmonic_sum = sum(1/i for i in range(1, n + 1))
    return fuel * harmonic_sum

# Compare both methods
n = 4
fuel = 100

iterative_result = maximum_distance(n, fuel)
formula_result = maximum_distance_formula(n, fuel)

print(f"Iterative method: {iterative_result:.2f} km")
print(f"Formula method: {formula_result:.2f} km")
print(f"Results match: {abs(iterative_result - formula_result) < 0.001}")
Iterative method: 208.33 km
Formula method: 208.33 km
Results match: True

Comparison

Number of Bikes Parallel Strategy Optimal Strategy Improvement
1 100.00 km 100.00 km 0%
2 100.00 km 150.00 km 50%
3 100.00 km 183.33 km 83%
4 100.00 km 208.33 km 108%

Conclusion

The optimal bike fuel transfer strategy uses the harmonic series to maximize distance coverage. By strategically transferring fuel at calculated intervals, we can achieve significantly more distance than running all bikes in parallel.

Updated on: 2026-03-25T09:31:10+05:30

624 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements