Total Distance Traveled
EasyPrompt
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 <= 100
Approaches
2 approaches with complexity analysis and trade-offs.
This approach directly simulates the process described in the problem. We use a loop to model the truck's journey in chunks. In each step, the truck consumes 5 liters of fuel, and if the additional tank is not empty, 1 liter is transferred back to the main tank. This process repeats until the main tank has less than 5 liters, after which the remaining fuel is consumed to cover the final distance.
Algorithm
-
- Initialize a variable
distanceto 0.
- Initialize a variable
-
- Start a
whileloop that continues as long asmainTankhas 5 or more liters.
- Start a
-
- Inside the loop, simulate the consumption of 5 liters:
- Decrement
mainTankby 5. - Increment
distanceby 50 (since mileage is 10 km/l).
-
- Check if the
additionalTankhas fuel (additionalTank > 0).
- If it does, transfer 1 liter by incrementing
mainTankby 1 and decrementingadditionalTankby 1.
- Check if the
-
- Once the loop finishes,
mainTankwill have less than 5 liters.
- Once the loop finishes,
-
- Calculate the distance from the remaining fuel by adding
mainTank * 10todistance.
- Calculate the distance from the remaining fuel by adding
-
- Return the total
distance.
- Return the total
Walkthrough
The simulation approach iteratively calculates the total distance. We track the fuel in both tanks and the total distance traveled. The core of the logic is a while loop that executes as long as the mainTank contains at least 5 liters, which is the threshold for a potential fuel transfer.
Inside the loop, we subtract 5 liters from the mainTank and add 50 km to our total distance. Then, we check the additionalTank. If it has fuel, we simulate the transfer of 1 liter to the mainTank. This cycle of consuming 5 liters and potentially gaining 1 liter continues. After the loop terminates, any fuel left in the mainTank (which will be less than 5 liters) is used up, and the corresponding distance is added to the total.
class Solution { public int distanceTraveled(int mainTank, int additionalTank) { int distance = 0; while (mainTank >= 5) { // Consume 5 liters from the main tank mainTank -= 5; distance += 50; // Transfer 1 liter from additional tank if possible if (additionalTank > 0) { mainTank += 1; additionalTank -= 1; } } // Add distance from remaining fuel in the main tank distance += mainTank * 10; return distance; }}Complexity
Time
O(mainTank). In each iteration of the loop, the `mainTank`'s fuel decreases by a net of 4 liters (5 consumed, 1 added). Therefore, the number of iterations is proportional to the initial amount of fuel in the main tank. Given the constraint `mainTank <= 100`, this is very fast.
Space
O(1). The algorithm uses a fixed number of variables to store the state (`mainTank`, `additionalTank`, `distance`), so the space complexity is constant.
Trade-offs
Pros
It is straightforward to understand and implement as it directly follows the problem statement.
It is guaranteed to be correct for the given constraints.
Cons
Less efficient than a direct mathematical solution because it involves a loop.
For significantly larger constraints, this approach might become too slow.
Solutions
Solution
class Solution { public int distanceTraveled ( int mainTank , int additionalTank ) { int ans = 0 , cur = 0 ; while ( mainTank > 0 ) { cur ++; ans += 10 ; mainTank --; if ( cur % 5 == 0 && additionalTank > 0 ) { additionalTank --; mainTank ++; } } return ans ; } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.