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.
Input: mainTank =5, additionalTank =10Output: 60Explanation:
After spending 5 litre of fuel, fuel remaining is(5-5+1)=1 litre and distance traveled is50km.After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty.Total distance traveled is60km.
Input: mainTank =1, additionalTank =2Output: 10Explanation:
After spending 1 litre of fuel, the main tank becomes empty.Total distance traveled is10km.
Simulate the process: for every 5 liters used from the main tank, if possible, transfer 1 liter from the additional tank. Count total liters used and multiply by 10 for distance.
While mainTank >= 5 and additionalTank > 0, use 5 liters, transfer 1 from additionalTank. When mainTank < 5, use up the rest. Total distance is (all liters used) * 10.
classSolution {
public:int distanceTraveled(int mainTank, int additionalTank) {
int used =0;
while (mainTank >=5&& additionalTank >0) {
mainTank -=5;
used +=5;
mainTank +=1;
additionalTank -=1;
}
used += mainTank;
return used *10;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintdistanceTraveled(int mainTank, int additionalTank) {
int used = 0;
while (mainTank >= 5 && additionalTank > 0) {
mainTank -= 5;
used += 5;
mainTank += 1;
additionalTank -= 1;
}
used += mainTank;
return used * 10;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defdistanceTraveled(self, mainTank: int, additionalTank: int) -> int:
used =0while mainTank >=5and additionalTank >0:
mainTank -=5 used +=5 mainTank +=1 additionalTank -=1 used += mainTank
return used *10
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfndistance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 {
letmut used =0;
while main_tank >=5&& additional_tank >0 {
main_tank -=5;
used +=5;
main_tank +=1;
additional_tank -=1;
}
used += main_tank;
used *10 }
}