Total Distance Traveled - Problem

Imagine you're a truck driver on a long cross-country journey with a unique dual-tank fuel system! Your truck has two separate fuel tanks: a main tank that powers the engine, and an additional tank that serves as a backup reservoir.

Here's how this fascinating system works:

  • ๐Ÿš› Your truck gets 10 km per liter of fuel efficiency
  • โ›ฝ The engine only runs on fuel from the main tank
  • ๐Ÿ”„ Every time you consume exactly 5 liters from the main tank, the system automatically transfers 1 liter from the additional tank to the main tank (if available)
  • โšก The fuel transfer is instantaneous and happens immediately when you hit each 5-liter consumption milestone

Given the initial fuel amounts in both tanks, your goal is to calculate the maximum total distance you can travel before running out of fuel completely.

Example: If you start with 5 liters in the main tank and 10 liters in the additional tank, you'll travel 50 km using the initial 5 liters, then receive 1 liter from the additional tank, allowing you to continue your journey!

Input & Output

example_1.py โ€” Basic Case
$ Input: mainTank = 5, additionalTank = 10
โ€บ Output: 60
๐Ÿ’ก Note: We start with 5L in main tank. After consuming all 5L (traveling 50km), we trigger one transfer of 1L from additional tank. This 1L allows us to travel 10 more km, giving us a total of 60km.
example_2.py โ€” Multiple Transfers
$ Input: mainTank = 1, additionalTank = 2
โ€บ Output: 10
๐Ÿ’ก Note: We have only 1L in main tank, so we can travel 10km. Since we consumed less than 5L total, no transfer from additional tank occurs. The additional tank fuel remains unused.
example_3.py โ€” No Additional Fuel
$ Input: mainTank = 25, additionalTank = 0
โ€บ Output: 250
๐Ÿ’ก Note: With 25L in main tank and 0L in additional tank, we can only use the main tank fuel. 25L ร— 10km/L = 250km total distance.

Constraints

  • 1 โ‰ค mainTank, additionalTank โ‰ค 1000
  • The truck's fuel efficiency is always 10 km per liter
  • Transfer only occurs when exactly 5 liters total have been consumed from the main tank
  • Each transfer moves exactly 1 liter from additional to main tank

Visualization

Tap to expand
Main TankAdditional5L1LAUTODistance CalculatorFuel Efficiency: 10 km/LTransfer Rate: 5L โ†’ 1LTotal Distance = (Main + Transfers) ร— 10TRUCK
Understanding the Visualization
1
Journey Begins
Truck starts with fuel in both main and additional tanks
2
Fuel Consumption
Engine consumes fuel from main tank at 10km per liter
3
Transfer Trigger
Every 5 liters consumed triggers automatic 1L transfer
4
Journey Continues
Process repeats until no more fuel can be transferred
5
Journey Ends
Calculate total distance when main tank is finally empty
Key Takeaway
๐ŸŽฏ Key Insight: Instead of simulating every liter, we can mathematically calculate the total transfers by recognizing that every 5 liters consumed from the main tank triggers exactly 1 liter transfer from the additional tank!
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
42.0K Views
Medium Frequency
~15 min Avg. Time
1.9K 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