Skip to main content
Back to problems
#2739
Easy Algorithms

Total distance traveled

Math Simulation
41.2% acceptance
Feb 25, 2026
326
107
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn distance_traveled(main_tank: i32, additional_tank: i32) -> i32 {
    let mut main = main_tank;
    let mut add = additional_tank;
    let mut dist = 0;
    while main >= 5 {
      dist += 50;
      main -= 5;
      if add > 0 { main += 1; add -= 1; }
    }
    dist + main * 10
  }
}