Sort by

recency

|

1278 Discussions

|

  • + 0 comments

    Here is my easy c++ solution , you can watch the explanation here : https://youtu.be/RZti_qPbiAA

    int chocolateFeast(int n, int c, int m) {
        int result = 0, wrappers = 0;
        while( n >= c){
            result ++;
            wrappers ++;
            n -= c;
            if(wrappers == m){
                result ++;
                wrappers = 1;
            }
        }
        return result;
    }
    
  • + 0 comments

    Java: b for bars, w for wrappers

     public static int chocolateFeast(int n, int c, int m) {
            int b = n/c;
            int w = b;
            
            while (w >= m) {
                b += w/m;
                w = w%m + w/m;
            }
            
            return b;
        }
    
  • + 0 comments

    python solution

    recursive

    def recursiveChocolateFeast(wrapper,m):
        if wrapper<m:
            return 0
        return wrapper//m +recursiveChocolateFeast(wrapper//m+wrapper%m,m)
        
    def chocolateFeast(n, c, m):
        return n//c +recursiveChocolateFeast(n//c,m)
    

    non recursive

    def chocolateFeast(n, c, m):
        # Write your code here
        res =n//c
        wrapper =res
        while wrapper>=m:
            temp = wrapper%m
            wrapper //=m
    				res +=wrapper
            wrapper +=temp
    		return res
    
    return res
    
  • + 0 comments

    My solution in Python 3:

    def chocolateFeast(n, c, m):
        # Write your code here
        #n is the number of money Bobby has, he uses once
        #Bobby takes full advantage of promotion, hence, wrappers are used many times
            
        #Steps:
        '''
            1. Calculate intial bars, also intial wrappers.
            2. Create a loop, until Bobby cant trade, that updates bars and new wrappers (remain ones + traded ones)
            3. Return result
                
        '''
        
        bars: int = n / c 
        remain_wrappers: int = bars
        
        while remain_wrappers >= m:
            trade: int = remain_wrappers // m
            remain_wrappers = remain_wrappers % m + trade
            
            bars += trade
            
        return int(bars)
    
  • + 0 comments

    My solution in Java 8:

    public static int chocolateFeast(int n, int c, int m) {
        // Write your code here
            //n is the number of money Bobby has, he uses once
            //Bobby takes full advantage of promotion, hence, wrappers are used many times
            
            //Steps:
            /*
                1. Calculate intial bars, also intial wrappers.
                2. Create a loop, until Bobby cant trade, that updates bars and new wrappers (remain ones + traded ones)
                3. Return result
                
            */
            
            int bars = n / c; //Initial bars
            int remainWrapper = bars; //Inital wrappers 
            
            while(remainWrapper >= m){
                //While he can trade
                int trade = remainWrapper / m;
                remainWrapper = remainWrapper - trade * m + trade;
                //remainWrapper = remainWrapper % m + trade;
                
                bars += trade;
            }
            
            return bars;
        }