Camel and bananas - rFronteddu/general_wiki GitHub Wiki
A person has 3000 bananas and a camel. The person wants to transport the maximum number of bananas to a destination which is 1000 KMs away, using only the camel as a mode of transportation. The camel cannot carry more than 1000 bananas at a time and eats a banana every km it travels. What is the maximum number of bananas that can be transferred to the destination using only camel (no other mode of transportation is allowed).
A person won’t be able to transfer any banana directly to the destination as the camel is going to eat all the banana on its way to the destination.
The trick is to have intermediate drop points, then, the camel can make several short trips in between.
Also, we try to maintain the number of bananas at each point to be multiple of 1000.
Let’s have 2 drop points in between the source and destination, DP1 and DP2.
With 3000 bananas at the source. 2000 at DP1 and 1000 at DP2.
* To go from source to DP1, the camel has to take a total of 5 trips 3 forward and 2 backward since we have 3000 bananas to transport.
* The same way from DP1 to DP2 the camel has to take a total of 3 trips, 2 forward and 1 backward. Since we have 2000 bananas to transport.
* Finally from DP2 to a destination, the camel needs to only move forward one time.
Let’s see the total number of bananas consumed at every point.
* From the source to DP1 it's 5*x bananas, as the distance between the source and DP1 is x km and the camel had 5 trips.
* From DP1 to DP2 its 3*y bananas, as the distance between DP1 and DP2 is y km and the camel had 3 trips.
* From IP2 to destination it's z bananas.
We now try to calculate the distance between the points:
* 3000 – 5x = 2000 so we get x = 200
* 2000-3y = 1000 so we get y = 333.33, we need to round so we take y = 333. At DP2 we have the number of bananas equal 1001, so its 2000-3y = 1001
* The remaining distance to the market is 1000 - x - y = z => 1000 - 200 - 333 => z = 467.
Now, there are 1001 bananas at DP2. However the camel can carry only 1000 bananas at a time, so we need to leave one banana behind.
So from DP2 to the destination point camel eats 467 bananas. The remaining bananas are 1000 - 467 = 533.