A person wants to transfer bananas over to a destination A km away. He initially has B bananas and a camel. The camel cannot carry more than C bananas at a time and eats a banana every km it travels. Given three integers A, B, and C, the task is to find the maximum number of bananas the person can transfer to the destination using the camel.
Note: The given problem is a generalized version of the famous Camel-Banana puzzle.
Examples:
Input: A = 10, B = 30, C = 10Â
Output: 5Input: A = 1000, B = 3000, C = 1000
Output: 533
Approach: The given problem can be solved with the help of Dynamic Programming using Memoization using the following key points:
- It can be observed that the most effective way to transfer bananas is to divide the path (u, v) having A km into some smaller parts. Suppose x is a breakpoint in the path (u, v). The optimal choice is to transfer all the bananas from u to x and then from x to v.
- There can be any number of breakpoints in the path (u, v) such that the count of breakpoints < A.
- The total number of trips the camel which can carry C bananas at a time has to make in order to transfer X bananas over any distance can be calculated by the formula 2 * X / C – 1, if C is a factor of X (i.e, X % C = 0) otherwise 2 * X / C +1.
 Using the above observations, the given problem can be solved by following the below steps:
- Consider a 2D array dp[][], where a state dp[A][B] represents the maximum number of bananas a camel can transfer over a distance of A km having B bananas initially. Initialize the dp[][] array with -1.
- Create a recursive function to iterate over the given path of A km and create a breakpoint at each valid index and recursively call the function for the remaining path.
- Memoize the maximum number of bananas for each state and return the memoized value if the current state is already calculated.
Below is the implementation of the above approach:
C++
| // C++ program of the above approach#include <bits/stdc++.h>usingnamespacestd;  // Stores the overlapping stateintdp[1001][3001];  // Recursive function to find the maximum// number of bananas that can be transferred// to A distance using memoizationintrecBananaCnt(intA, intB, intC){    // Base Case where count of bananas    // is less that the given distance    if(B <= A) {        return0;    }      // Base Case where count of bananas    // is less that camel's capacity    if(B <= C) {        returnB - A;    }      // Base Case where distance = 0    if(A == 0) {        returnB;    }      // If the current state is already    // calculated    if(dp[A][B] != -1) {        returndp[A][B];    }      // Stores the maximum count of bananas    intmaxCount = INT_MIN;      // Stores the number of trips to transfer    // B bananas using a camel of capacity C    inttripCount = B % C == 0 ? ((2 * B) / C) - 1                               : ((2 * B) / C) + 1;      // Loop to iterate over all the    // breakpoints in range [1, A]    for(inti = 1; i <= A; i++) {          // Recursive call over the        // remaining path        intcurCount            = recBananaCnt(A - i,                           B - tripCount * i, C);          // Update the maxCount        if(curCount > maxCount) {            maxCount = curCount;              // Memoize the current value            dp[A][B] = maxCount;        }    }      // Return answer    returnmaxCount;}  // Function to find the maximum number of// bananas that can be transferredintmaxBananaCnt(intA, intB, intC){    // Initialize dp array with -1    memset(dp, -1, sizeof(dp));      // Function Call    returnrecBananaCnt(A, B, C);}  // Driver Codeintmain(){    intA = 1000;    intB = 3000;    intC = 1000;    cout << maxBananaCnt(A, B, C);      return0;} | 
Java
| // Java program of the above approachpublicclassGFG {    Â    // Stores the overlapping state    finalstaticintdp[][] = newint[1001][3001];      // Recursive function to find the maximum    // number of bananas that can be transferred    // to A distance using memoization    staticintrecBananaCnt(intA, intB, intC)    {   Â                Â        // Base Case where count of bananas        // is less that the given distance        if(B <= A) {            return0;        }    Â        // Base Case where count of bananas        // is less that camel's capacity        if(B <= C) {            returnB - A;        }    Â        // Base Case where distance = 0        if(A == 0) {            returnB;        }    Â        // If the current state is already        // calculated        if(dp[A][B] != -1) {            returndp[A][B];        }    Â        // Stores the maximum count of bananas        intmaxCount = Integer.MIN_VALUE;    Â        // Stores the number of trips to transfer        // B bananas using a camel of capacity C        inttripCount = B % C == 0? ((2* B) / C) - 1: ((2* B) / C) + 1;    Â        // Loop to iterate over all the        // breakpoints in range [1, A]        for(inti = 1; i <= A; i++) {    Â            // Recursive call over the            // remaining path            intcurCount                = recBananaCnt(A - i,                               B - tripCount * i, C);    Â            // Update the maxCount            if(curCount > maxCount) {                maxCount = curCount;    Â                // Memoize the current value                dp[A][B] = maxCount;            }        }    Â        // Return answer        returnmaxCount;    }    Â    // Function to find the maximum number of    // bananas that can be transferred    staticintmaxBananaCnt(intA, intB, intC)    {        // Initialize dp array with -1        for(inti = 0; i < 1001; i++)            for(intj = 0; j < 3001; j++)                dp[i][j] = -1;    Â        // Function Call        returnrecBananaCnt(A, B, C);    }      // Driver Code    publicstaticvoidmain (String[] args) {        Â            intA = 1000;            intB = 3000;            intC = 1000;            System.out.println(maxBananaCnt(A, B, C));    }}  // This code is contributed by AnkThon | 
Python3
| # Python program of the above approach# Stores the overlapping statedp =[[-1fori inrange(3001)] forj inrange(1001)]  # Recursive function to find the maximum# number of bananas that can be transferred# to A distance using memoizationdefrecBananaCnt(A, B, C):      # Base Case where count of bananas    # is less that the given distance    if(B <=A):        return0        Â    # Base Case where count of bananas    # is less that camel's capacity    if(B <=C):        returnB -A    Â    # Base Case where distance = 0    if(A ==0):        returnB    Â      # If the current state is already    # calculated    if(dp[A][B] !=-1):        returndp[A][B]    Â      # Stores the maximum count of bananas    maxCount =-2**32      # Stores the number of trips to transfer    # B bananas using a camel of capacity C    tripCount =((2*B) //C) -1if(B %C ==0) else((2*B) //C) +1      # Loop to iterate over all the    # breakpoints in range [1, A]    fori inrange(1,A+1):          # Recursive call over the        # remaining path        curCount =recBananaCnt(A -i, B -tripCount *i, C)          # Update the maxCount        if(curCount > maxCount):            maxCount =curCount              # Memoize the current value            dp[A][B] =maxCount        Â    # Return answer    returnmaxCount  # Function to find the maximum number of# bananas that can be transferreddefmaxBananaCnt(A, B, C):      # Function Call    returnrecBananaCnt(A, B, C)  # Driver CodeA =1000B =3000C =1000print(maxBananaCnt(A, B, C))  # This code is contributed by shivanisinghss2110 | 
C#
| // C# program of the above approachusingSystem;  publicclassGFG {      // Stores the overlapping state    staticint[, ] dp = newint[1001, 3001];      // Recursive function to find the maximum    // number of bananas that can be transferred    // to A distance using memoization    staticintrecBananaCnt(intA, intB, intC)    {          // Base Case where count of bananas        // is less that the given distance        if(B <= A) {            return0;        }          // Base Case where count of bananas        // is less that camel's capacity        if(B <= C) {            returnB - A;        }          // Base Case where distance = 0        if(A == 0) {            returnB;        }          // If the current state is already        // calculated        if(dp[A, B] != -1) {            returndp[A, B];        }          // Stores the maximum count of bananas        intmaxCount = Int32.MinValue;          // Stores the number of trips to transfer        // B bananas using a camel of capacity C        inttripCount = B % C == 0 ? ((2 * B) / C) - 1                                   : ((2 * B) / C) + 1;          // Loop to iterate over all the        // breakpoints in range [1, A]        for(inti = 1; i <= A; i++) {              // Recursive call over the            // remaining path            intcurCount                = recBananaCnt(A - i, B - tripCount * i, C);              // Update the maxCount            if(curCount > maxCount) {                maxCount = curCount;                  // Memoize the current value                dp[A, B] = maxCount;            }        }          // Return answer        returnmaxCount;    }      // Function to find the maximum number of    // bananas that can be transferred    staticintmaxBananaCnt(intA, intB, intC)    {      Â        // Initialize dp array with -1        for(inti = 0; i < 1001; i++)            for(intj = 0; j < 3001; j++)                dp[i, j] = -1;          // Function Call        returnrecBananaCnt(A, B, C);    }      // Driver Code    publicstaticvoidMain(string[] args)    {          intA = 1000;        intB = 3000;        intC = 1000;        Console.WriteLine(maxBananaCnt(A, B, C));    }}  // This code is contributed by ukasp. | 
Javascript
| <script>       // JavaScript Program to implement       // the above approach         // Stores the overlapping state       // Initialize dp array with -1       let dp = newArray(1001);       for(let i = 0; i < dp.length; i++)       {           dp[i] = (newArray(3001).fill(-1))       }         // Recursive function to find the maximum       // number of bananas that can be transferred       // to A distance using memoization       functionrecBananaCnt(A, B, C)        {       Â           // Base Case where count of bananas           // is less that the given distance           if(B <= A) {               return0;           }             // Base Case where count of bananas           // is less that camel's capacity           if(B <= C) {               returnB - A;           }             // Base Case where distance = 0           if(A == 0) {               returnB;           }             // If the current state is already           // calculated           if(dp[A][B] != -1) {               returndp[A][B];           }             // Stores the maximum count of bananas           let maxCount = Number.MIN_VALUE;             // Stores the number of trips to transfer           // B bananas using a camel of capacity C           let tripCount = B % C == 0 ? Math.floor((2 * B) / C) - 1               : Math.floor((2 * B) / C) + 1;             // Loop to iterate over all the           // breakpoints in range [1, A]           for(let i = 1; i <= A; i++) {                 // Recursive call over the               // remaining path               let curCount                   = recBananaCnt(A - i,                       B - tripCount * i, C);                 // Update the maxCount               if(curCount > maxCount) {                   maxCount = curCount;                     // Memoize the current value                   dp[A][B] = maxCount;               }           }             // Return answer           returnmaxCount;       }         // Function to find the maximum number of       // bananas that can be transferred       functionmaxBananaCnt(A, B, C) {           // Function Call           returnrecBananaCnt(A, B, C);       }         // Driver Code       let A = 1000;       let B = 3000;       let C = 1000;       document.write(maxBananaCnt(A, B, C));      // This code is contributed by Potta Lokesh   </script> | 
533
Â
Time Complexity: O(A*A*B)
Auxiliary Space: O(A*B)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







