A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters.
The truck has a mileage of 10 km per liter. Whenever 5 liters of fuel get used up in the main tank, if the additional tank has at least 1 liters of fuel, 1 liters of fuel will be transferred from the additional tank to the main tank.
Return the maximum distance which can be traveled.
Note: Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.
Example 1:
Input: mainTank = 5, additionalTank = 10 Output: 60 Explanation: After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. Total distance traveled is 60km.
Example 2:
Input: mainTank = 1, additionalTank = 2 Output: 10 Explanation: After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km.
Constraints:
1 <= mainTank, additionalTank <= 100Problem Overview: You are given two integers: mainTank and additionalTank. A truck consumes 1 liter of fuel for every 10 km traveled. Each time the truck uses 5 liters from the main tank, you can transfer 1 liter from the additional tank to the main tank (if available). The goal is to compute the total distance the truck can travel.
Approach 1: Simulation Approach (Time: O(n), Space: O(1))
This method follows the exact process described in the problem using simulation. You repeatedly consume fuel from the main tank while tracking how many liters have been used. After every 5 liters consumed, check whether the additional tank still has fuel. If it does, transfer 1 liter to the main tank and continue driving. The loop continues until the main tank becomes empty. The final distance equals the total liters consumed multiplied by 10. This approach mirrors the real-world process step by step and is useful when verifying the mechanics of the rule or when constraints are small.
Approach 2: Direct Calculation Approach (Time: O(1), Space: O(1))
The fuel transfer rule reveals a mathematical pattern. For every 5 liters consumed from the main tank, you gain 1 extra liter from the additional tank. However, the last few liters may not complete another group of 5, so they do not trigger another transfer. The number of possible transfers is therefore (mainTank - 1) / 4 using integer division. This works because each transfer effectively increases usable fuel before the next group of five is completed. The actual number of transferred liters is limited by additionalTank, so the formula becomes extra = min(additionalTank, (mainTank - 1) // 4). The total usable fuel becomes mainTank + extra, and the final distance is that value multiplied by 10.
This approach relies on recognizing the repeating pattern rather than explicitly simulating each step. It uses simple math and constant-time arithmetic, which makes it significantly cleaner and faster.
Recommended for interviews: The direct calculation approach is usually what interviewers expect. Simulation demonstrates that you understand the mechanics of the rule, but recognizing the mathematical pattern and reducing the solution to O(1) time shows stronger problem-solving and optimization skills.
This approach involves simulating the consumption of fuel from the main tank and the transfer of fuel from the additional tank when the main tank consumes 5 liters. We continue this process until the fuel in the main tank is completely depleted and no more can be injected from the additional tank.
This C code implements a simulation by decrementing the fuel in the main tank in chunks. If 5 liters are consumed and there is fuel in the additional tank, 1 liter is injected into the main tank. Iterations continue until the main tank is empty.
Time Complexity: O(mainTank), as each iteration represents either 1 or 5 liters consumed until mainTank is empty.
Space Complexity: O(1), as a constant amount of space is used for variables.
This approach calculates the maximum travelable distance directly using arithmetic by simulating potential transfers in a streamlined calculation. Instead of iterating, it computes the total number of 5-liter chunks that can be used, applying additional transfers one at a time as allowable by the additional tank.
This C solution calculates how many whole 5-liter chunk consumptions can be made from the main tank, adjusting for how many fuel transfers are possible from the additional tank simultaneously.
Time Complexity: O(1).
Space Complexity: O(1).
We can simulate the process of the truck's movement. Each time, it consumes 1 liter of fuel from the main fuel tank and travels 10 kilometers. Whenever the fuel in the main fuel tank is consumed by 5 liters, if there is fuel in the auxiliary fuel tank, 1 liter of fuel is transferred from the auxiliary fuel tank to the main fuel tank. The simulation continues until the fuel in the main fuel tank is exhausted.
The time complexity is O(n + m), where n and m are the amounts of fuel in the main and auxiliary fuel tanks, respectively. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Simulation Approach | Time Complexity: O(mainTank), as each iteration represents either 1 or 5 liters consumed until mainTank is empty. |
| Direct Calculation Approach | Time Complexity: O(1). |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation | O(n) | O(1) | Useful when implementing the process exactly as described or validating the transfer logic step-by-step |
| Direct Calculation (Math Formula) | O(1) | O(1) | Best choice for interviews and production code since the pattern allows constant-time computation |
Leetcode | 2739. Total Distance Traveled | Easy | Java Solution • Developer Docs • 805 views views
Watch 9 more video solutions →Practice Total Distance Traveled with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor