Total Distance Traveled - Problem

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 liter of fuel, 1 liter 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.

Input & Output

Example 1 — Basic Transfer
$ Input: mainTank = 5, additionalTank = 10
Output: 60
💡 Note: Main tank has 5 liters. After using 5 liters (50 km), we can transfer 1 liter from additional tank. Total fuel used: 5 + 1 = 6 liters. Distance: 6 × 10 = 60 km.
Example 2 — Multiple Transfers
$ Input: mainTank = 25, additionalTank = 6
Output: 310
💡 Note: Main tank has 25 liters. The truck uses fuel and transfers occur every 5 liters used: 5 transfers happen at 5L, 10L, 15L, 20L, 25L used, and one more at 30L used (from transferred fuel), totaling 6 transfers. Total fuel used: 25 + 6 = 31 liters. Distance: 31 × 10 = 310 km.
Example 3 — No Additional Fuel
$ Input: mainTank = 4, additionalTank = 0
Output: 40
💡 Note: No additional fuel available, so we can only use the 4 liters in main tank. Distance: 4 × 10 = 40 km.

Constraints

  • 1 ≤ mainTank, additionalTank ≤ 100

Visualization

Tap to expand
Total Distance Traveled INPUT MAIN 5L ADDITIONAL 10L mainTank = 5 additionalTank = 10 mileage = 10 km/L ALGORITHM STEPS 1 Initialize distance = 0, main = 5 2 Loop While main > 0 Consume 1L, add 10km 3 Check Every 5L Used Transfer 1L if add > 0 4 Return Total Distance Sum all km traveled Simulation: Round Main Add Dist 1 5-5 10-1 50km +1L from add tank 2 1-1 9 60km Total: 6L = 60km FINAL RESULT 0 60 OUTPUT 60 kilometers Breakdown: Main: 5L x 10 = 50km Transfer: 1L x 10 = 10km Total: 6L = 60km Key Insight: Every 5 liters consumed from main tank triggers a 1 liter transfer from additional tank (if available). This creates a bonus fuel effect: for every 5L used, you effectively get 6L worth of distance (5+1). Formula: Total Fuel = mainTank + min(additionalTank, mainTank/5) --> Distance = Total Fuel x 10 TutorialsPoint - Total Distance Traveled | Optimal Solution
Asked in
Microsoft 15 Apple 8
12.5K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen