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 litre to cover 1 km.
If we take a closer look at 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 kms. To generalize it more, when we have x litre remaining fuel in every bike and n remaining bikes, we drop a bike after x/n kms.
Read full article from Find the maximum distance covered using n bikes | GeeksforGeeks
If we take a closer look at 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 kms. To generalize it more, when we have x litre remaining fuel in every bike and n remaining bikes, we drop a bike after x/n kms.
double
maxDistance(
int
n,
int
fuel)
{
// dist_covered is the result of this function
double
dist_covered = 0;
while
(n > 0)
{
// after ever fuel/n km we are discarding one bike and filling
// all the other bikes with fuel/n liters of fuel i.e. to their
// maximum limit (100 litre)
dist_covered += (
double
)fuel / n;
n -= 1;
// reduce number of bikes
}
return
dist_covered;
}