Question

link

There’s a elephant, which can carry max 1000 bananas. The elephant eats a banana every 1 Km (both forward and back).

Now we want to transfer 3000 bananas to a place 1000 Km away. How many bananas can be left?

Also solved to generalized problem (write code for solution).

Analysis

If we subdivide distances for each kilometer. Notice if elephant wants to shift all the bananas 1 km, you will loose 5 bananas every km.

So we transferred 2995 (998+998+999) to one km distance. This process continues until after 200 km, we have only 2000 bananas left with remaining distance of 800 km.

Start from here, we only loose 3 bananas every km. This goes on for another 334 km, we will have 998 bananas left, and the rest of the bananas can be transfered in a single journey.

Solution

532 bananas.