Question
There are n bikes, and each can cover 100 km when fully fueled. What is the maximum amount of distance you can go using n
There are n bikes, and each can cover 100 km when fully fueled. What is the maximum amount of distance you can go using n bikes? You may assume that all bikes are similar, and a bike takes 1 liter to cover 1 km. You have n bikes and using one bike you can only cover 100 km. so if n bikes start from the same point and run simultaneously you can go only 100 km. Lets think a bit differently, trick is when you want to cover the maximum distance, you should always try to waste minimum fuel. Minimum wastage of fuel means to run a minimum number of bikes. Instead of the parallel running of n bikes, you can think of serially running them. That means if you transfer some amount of fuel from the last bike to other bikes and throw the last bike i.e., dont run the last bike after a certain point. But the question is, after what distance the fuel transfer must be done so that the maximum distance is covered, and the fuel tank of the remaining bikes do not overflow. Let us take the following base cases and then generalize the solution. Base Case 1: There is one bike: This is simple, we can cover 100 km only. Base Case 2: There are two bikes: What is the maximum distance we can cover when there are 2 bikes? To maximize the distance, we must drop the second bike at some point and transfer its fuel to the first bike. Let us do the transfer after x km. Total distance covered = Distance covered by 100 ltr in first bike + Distance covered by fuel transferred from the first bike. The remaining fuel in the second bike is 100 x. If we transfer this much fuel to the first bike, then the total distance would become 100 + 100 x which is 200 x. So, our task is to maximize 200-x. The constraint is, 100 x must be less than or equal to the space created in the first bike after x km, i.e., 100 x <= x. The value of 200 - x becomes maximum when x is minimum. The minimum possible value of x is 50. So, we can travel 150 km. Base Case 3: There are three bikes: Let the first transfer is done after x km. After x distance, all bikes contain the 100 - x amount of fuel. If we take 100 - x amount of fuel from 3rd bike and distribute it among 1st and 2nd bike so that fuel tanks of 1st and 2nd bikes get full. So 100-x <= 2*x; or, x=33.333 so we should transfer the remaining fuel of the third bike and distribute that amount of fuel among the 1st and 2nd bike after exactly 33.33 km. Let us generalize it. If we take a closer look at the above cases, we can observe that if there are n bikes, then the first transfer is done (or a bike is dropped) after 100/n km. To generalize it more, when we have x liter remaining fuel in every bike and n remaining bikes, we drop a bike after x/n km. Your program must have the following functions Input function for taking input (input is number of bikes, and amount of fuel (lets say Page 6 of 6 100 initially)) Output function (display the max distance covered) Double MaxDistance(int &n, int fuel) Example Output: The maximum distance possible with 5 bikes is 1141.666667.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started