This is a proper solution and explanation with a C++ code for the famous Camel Banana Puzzle.
Camel Banana is a puzzle that has many constraints, and here is the problem defined.
Question: The problem revolves around a scenario where a camel has to travel a certain number of bananas i.e 3000 from a point to another point 1000km away. To have constraints it said that camel can transfer a maximum of 1000 bananas at a time also camel eats 1 banana for each km it travels. What will be the maximum number of bananas transported to the endpoint?
Now we can say that even if the camel takes 1000 bananas and travels to the end then the camel will consume all the bananas and won’t be able to come back to starting point to transfer all the bananas.
Hence we will take a different approach.
There is a total of 3000 bananas and since the limit of load can be transported once is 1000, hence camel will have to take 3 trips in total to carry bananas from start to point A then will have to make 2 trips in total to get back to start after each trip.
So for transferring 3000 bananas 1km ahead camel will take 5 trips (3 trips to travel from the start point to point A and 2 trips to come back after each trip from starting to Point A), as shown in the figure from start to point A. Hence this will continue unless the bananas are less than 2000, so to consume 1000 bananas with each km being covered at the reduction of 5 bananas. The camel will cover
3000 - 5x = 2000 (This equation is to determine how many km camels traveled so that only 2000 km left)
Sow we get,
Now when less than 2000 bananas will be left then the Camel will be able to shift them to another point in 3 trips (2 trips to take bananas from point A to B and 1 trip to come back after the first trip) as shown in the figure and when the number of bananas left is less than 1000, then he won’t have to come back as bananas will be less than 1000 and that’s the maximum load it can take at once.
Now for the camel to transfer 2000 up to 1km it will take 3 trips from point A to B. so the camel with do 3 trips for each km unless the bananas are less than or equal to 1000. so the camel will cover y km till <1000 bananas are left, so
2000 - 3x = 1000 (This equation is to determine how many km camels traveled so that only 1000 km left)
So we get,
y = 333
Now since bananas are less than 1000 and that's the maximum load a camel can take hence the camel will have to travel only in the front direction and no need to come back.
The distance left to travel is,
1000 - (200 + 333) = 467
Hence camel travels 467 km and consumes 467 bananas out of the left 1000 bananas.
1000 - 467 = 533
Hence a total of 533 are maximum bananas that can be transferred from starting to end with all the constraints.