# Find the maximum distance covered using n bikes

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.

You have n bikes and using one bike you can only cover 100 km. so if n bikes start from same point and run simultaneously you can go only 100 km. Let’s think bit differently, trick is when you want to cover maximum distance, you should always try to waste minimum fuel. Minimum wastage of fuel means to run minimum number of bikes. Instead of parallel running of n bikes, you can think of serially running them. That means if you transfer some amount of fuel from last bike to another bikes and throw the last bike i.e., don’t run the last bike after certain point. But the question is, after what distance the fuel transfer has to be done so that the maximum distance is covered and fuel tank of remaining bikes do not overflow.

Let us take following base cases and then generalize the solution.

**Base Case 1: There is one bike:**This is simple, we can cover 100 kms 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 second bike at some point and transfer its fuel to first bike. Let we do the transfer after x kms.Total distance covered = Distance covered by 100 ltr in first bike + Distance covered by fuel transferred from first bike.

Remaining fuel in second bike is 100 – x. If we transfer this much fuel to 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 first bike after x kms, 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 are able to travel 150 kms.

**Base Case 3: There are three bikes :**Let the first transfer is done after x kms. After x distance all bikes contain 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 third bike and distribute that amount of fuel among 1st and 2nd bike after exactly 33.33 km.

Let us generalize it. 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.

Following is the implementation of a general function.

## C/C++

`#include <stdio.h> ` ` ` `// Returns maximum distance that can be traveled by n bikes and given fuel ` `// in every bike ` `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; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` `// number of bikes ` ` ` `int` `fuel = 100; ` ` ` `printf` `(` `"Maximum distance possible with %d bikes is %f"` `, ` ` ` `n, maxDistance(n, fuel)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum ` `// distance covered using n bikes ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns maximum distance that can be traveled by n bikes ` ` ` `// and given fuel in every bike ` ` ` `static` `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; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` `// number of bikes ` ` ` `int` `fuel = ` `100` `; ` ` ` `System.out.println(` `"Maximum distance possible with "` ` ` `+ n + ` `" bikes is "` `+ maxDistance(n, fuel)); ` ` ` `} ` `} ` ` ` `// Contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the maximum ` `# distance covered using n bikes ` ` ` `# Returns maximum distance that can be ` `# traveled by n bikes and given fuel ` `# in every bike ` `def` `maxDistance(n, fuel): ` ` ` ` ` `# dist_covered is the result ` ` ` `# of this function ` ` ` `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 ` `=` `dist_covered ` `+` `(fuel ` `/` `n) ` ` ` ` ` `# reduce number of bikes ` ` ` `n ` `=` `n ` `-` `1` ` ` ` ` `return` `dist_covered ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `3` ` ` ` ` `# number of bikes ` ` ` `fuel ` `=` `100` ` ` `print` `(` `"Maximum distance possible with"` `, ` ` ` `n, ` `"bikes is"` `, maxDistance(n, fuel)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the maximum ` `// distance covered using n bikes ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns maximum distance ` ` ` `// that can be travelled by n bikes ` ` ` `// and given fuel in every bike ` ` ` `static` `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; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// number of bikes ` ` ` `int` `n = 3; ` ` ` `int` `fuel = 100; ` ` ` `Console.WriteLine(` `"Maximum distance possible with "` `+ n + ` ` ` `" bikes is "` `+ maxDistance(n, fuel)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Returns maximum distance that can ` `// be traveled by n bikes and given ` `// fuel in every bike ` ` ` `function` `maxDistance(` `$n` `, ` `$fuel` `) ` `{ ` ` ` `// dist_covered is the result ` ` ` `// of this function ` ` ` `$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` `; ` ` ` ` ` `// reduce number of bikes ` ` ` `$n` `-= 1; ` ` ` `} ` ` ` `return` `$dist_covered` `; ` `} ` ` ` `// Driver Code ` ` ` `// number of bikes ` `$n` `= 3; ` `$fuel` `= 100; ` `echo` `"Maximum distance possible with "` `, ` ` ` `$n` `, ` `" bikes is "` `, ` ` ` `maxDistance(` `$n` `, ` `$fuel` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Maximum distance possible with 3 bikes is 183.333333

This article is contributed by **Shamik Mitra**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Find the distance covered to collect items at equal distances
- Find the maximum possible distance from origin using given points
- Find maximum distance between any city and station
- Maximum subsequence sum such that all elements are K distance apart
- Maximum distance between two 1's in Binary representation of N
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Find points at a given distance on a line of given slope
- Find the shortest distance between any pair of two different good nodes
- Haversine formula to find distance between two points on a sphere
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find distance of nodes from root in a tree for multiple queries
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree