Program for Chocolate and Wrapper Puzzle

16 Jul, 2021
Given the following three values, the task is to find the total number of maximum chocolates you can eat. 
money: Money you have to buy chocolatesprice: Price of a chocolatewrap: Number of wrappers to be returned for getting one extra chocolate.
money: Money you have to buy chocolates
price: Price of a chocolate
wrap: Number of wrappers to be returned for getting one extra chocolate.
It may be assumed that all given values are positive integers and greater than 1.Examples:  
Input: money = 16, price = 2, wrap = 2
Output:   15
Price of a chocolate is 2. You can buy 8 chocolates from
amount 16. You can return 8 wrappers back and get 4 more
chocolates. Then you can return 4 wrappers and get 2 more
chocolates. Finally you can return 2 wrappers to get 1
more chocolate.

Input:   money = 15, price = 1, wrap = 3
Output:   22
We buy and eat 15 chocolates
We return 15 wrappers and get 5 more chocolates.
We return 3 wrappers, get 1 chocolate and eat it
(keep 2 wrappers). Now we have 3 wrappers. Return
3 and get 1 more chocolate.
So total chocolates = 15 + 5 + 1 + 1

Input:  money = 20, price = 3, wrap = 5
Output:   7
Source: Puzzle 22 | (Maximum Chocolates)
A naive method is to continuously count the number of chocolates by returning wrappers until wrappers left didn’t become less than required to get a chocolate. 
Below is the implementation of this method. 
C++
Java
Python3
C#
PHP
Javascript
// Recursive C++ program to find maximum// number of chocolates#include <iostream>using namespace std; // Returns number of chocolates we can// have from given number of chocolates// and number of wrappers required to// get a chocolate.int countRec(int choc, int wrap){    // If number of chocolates is less than    // number of wrappers required.    if (choc < wrap)        return 0;     // We can immediately get newChoc using    // wrappers of choc.    int newChoc = choc/wrap;     // Now we have "newChoc + choc%wrap" wrappers.    return newChoc + countRec(newChoc + choc%wrap,                              wrap);} // Returns maximum number of chocolates we can eat// with given money, price of chocolate and number// of wrappers required to get a chocolate.int countMaxChoco(int money, int price, int wrap){    // We can directly buy below number of chocolates    int choc = money/price;     // countRec returns number of chocolates we can    // have from given number of chocolates    return choc + countRec(choc, wrap);} // Driver codeint main(){    int money = 15 ; // total money    int price = 1; // cost of each candy    int wrap = 3 ; // no of wrappers needs to be    // exchanged for one chocolate.     cout << countMaxChoco(money, price, wrap);    return 0;}
// Recursive java program to find maximum// number of chocolates import java.io.*; class GFG {     // Returns number of chocolates we can    // have from given number of chocolates    // and number of wrappers required to    // get a chocolate.    static int countRec(int choc, int wrap)    {                 // If number of chocolates is less than        // number of wrappers required.        if (choc < wrap)            return 0;             // We can immediately get newChoc using        // wrappers of choc.        int newChoc = choc / wrap;             // Now we have "newChoc + choc%wrap"        // wrappers.        return newChoc + countRec(newChoc +                          choc % wrap, wrap);    }         // Returns maximum number of chocolates    // we can eat with given money, price of    // chocolate and number of wrappers    // required to get a chocolate.    static int countMaxChoco(int money,                          int price, int wrap)    {                 // We can directly buy below number of        // chocolates        int choc = money/price;             // countRec returns number of chocolates        // we can have from given number of        // chocolates        return choc + countRec(choc, wrap);    }         // Driver code    public static void main (String[] args)    {        int money = 15 ; // total money        int price = 1; // cost of each candy                 // no of wrappers needs to be        // exchanged for one chocolate.        int wrap = 3 ;        System.out.println(            countMaxChoco(money, price, wrap));    }} // This code is contributed by anuj_67.
# Recursive Python3 program to find# maximum number of chocolatesimport math # Returns number of chocolates we can# have from given number of chocolates# and number of wrappers required to# get a chocolate.def countRec(choc, wrap):         # If number of chocolates is less    # than number of wrappers required.    if (choc < wrap):        return 0;     # We can immediately get newChoc    # using wrappers of choc.    newChoc = choc / wrap;     # Now we have "newChoc + choc%wrap" wrappers.    return newChoc + countRec(newChoc + choc %                                  wrap, wrap); # Returns maximum number of chocolates# we can eat with given money, price# of chocolate and number of wrappers# required to get a chocolate.def countMaxChoco(money, price, wrap):         # We can directly buy below    # number of chocolates    choc = money / price;     # countRec returns number    # of chocolates we can    # have from given number    # of chocolates    return math.floor(choc + countRec(choc, wrap)); # Driver code # total moneymoney = 15;     # cost of each candyprice = 1;     # no of wrappers needs to bewrap = 3 ;     # exchanged for one chocolate.print(countMaxChoco(money, price, wrap)); # This code is contributed by mits
// Recursive C# program to find maximum// number of chocolates using System; class GFG {     // Returns number of chocolates we can    // have from given number of chocolates    // and number of wrappers required to    // get a chocolate.    static int countRec(int choc, int wrap)    {                 // If number of chocolates is less than        // number of wrappers required.        if (choc < wrap)            return 0;             // We can immediately get newChoc using        // wrappers of choc.        int newChoc = choc / wrap;             // Now we have "newChoc + choc%wrap"        // wrappers.        return newChoc + countRec(newChoc +                        choc % wrap, wrap);    }         // Returns maximum number of chocolates    // we can eat with given money, price of    // chocolate and number of wrappers    // required to get a chocolate.    static int countMaxChoco(int money,                        int price, int wrap)    {                 // We can directly buy below number of        // chocolates        int choc = money/price;             // countRec returns number of chocolates        // we can have from given number of        // chocolates        return choc + countRec(choc, wrap);    }         // Driver code    public static void Main ()    {        int money = 15 ; // total money        int price = 1; // cost of each candy                 // no of wrappers needs to be        // exchanged for one chocolate.        int wrap = 3 ;        Console.WriteLine(            countMaxChoco(money, price, wrap));    }} // This code is contributed by anuj_67.
<?php// Recursive PHP program to find maximum// number of chocolates // Returns number of chocolates we can// have from given number of chocolates// and number of wrappers required to// get a chocolate.function countRec($choc, $wrap){         // If number of chocolates is less than    // number of wrappers required.    if ($choc < $wrap)        return 0;     // We can immediately get newChoc using    // wrappers of choc.    $newChoc = $choc/$wrap;     // Now we have "newChoc + choc%wrap" wrappers.    return $newChoc + countRec($newChoc + $choc % $wrap,                                                  $wrap);} // Returns maximum number of chocolates we can eat// with given money, price of chocolate and number// of wrappers required to get a chocolate.function countMaxChoco($money, $price, $wrap){         // We can directly buy below    // number of chocolates    $choc = $money/$price;     // countRec returns number    // of chocolates we can    // have from given number    // of chocolates    return floor($choc + countRec($choc, $wrap));}     // Driver code    // total money    $money = 15;         // cost of each candy    $price = 1;         // no of wrappers needs to be    $wrap = 3 ;         // exchanged for one chocolate.    echo countMaxChoco($money, $price, $wrap); // This code is contributed by anuj_67.?>
<script> // JavaScript program to find maximum// number of chocolates // Returns number of chocolates we can    // have from given number of chocolates    // and number of wrappers required to    // get a chocolate.    function countRec(choc, wrap)    {                  // If number of chocolates is less than        // number of wrappers required.        if (choc < wrap)            return 0;              // We can immediately get newChoc using        // wrappers of choc.        let newChoc = choc / wrap;              // Now we have "newChoc + choc%wrap"        // wrappers.        return newChoc + countRec(newChoc +                          choc % wrap, wrap);    }          // Returns maximum number of chocolates    // we can eat with given money, price of    // chocolate and number of wrappers    // required to get a chocolate.    function countMaxChoco(money,                          price, wrap)    {                  // We can directly buy below number of        // chocolates        let choc = money/price;              // countRec returns number of chocolates        // we can have from given number of        // chocolates        return choc + countRec(choc, wrap);    }    // Driver code          let money = 15 ; // total money        let price = 1; // cost of each candy                  // no of wrappers needs to be        // exchanged for one chocolate.        let wrap = 3 ;        document.write(Math.floor(            countMaxChoco(money, price, wrap)));             </script>
Output : 
22
An efficient solution is to use a direct formula to find the number of chocolates. 
Find initial number of chocolates by
dividing the amount with per piece cost.
i.e. choc = money / price

then apply below formula
choc += (choc - 1)/(wrap - 1)
In the above naive implementation, we noticed that after finding the initial number of chocolates, we recursively divide the number of chocolates by the number of wrappers required. until we left with 1 chocolate or wrapper. We are recomputing the values i.e. ((choc/wrap + choc%wrap)/wrap until we get 1. It is observed that we can get the result by just reducing the values of chocolates and wrappers by 1 and then divide them to get the result (choc-1)/(wrap-1) 
Below is the implementation of the above approach:
C++
Java
Python3
C#
PHP
Javascript
// Efficient C++ program to find maximum// number of chocolates#include <iostream>using namespace std; // Returns maximum number of chocolates we can eat// with given money, price of chocolate and number// of wrapprices required to get a chocolate.int countMaxChoco(int money, int price, int wrap){    // Corner case    if (money < price)       return 0;     // First find number of chocolates that    // can be purchased with the given amount    int choc = money / price;     // Now just add number of chocolates with the    // chocolates gained by wrapprices    choc = choc + (choc - 1) / (wrap - 1);    return choc;} // Driver codeint main(){    int money = 15 ; // total money    int price = 1; // cost of each candy    int wrap = 3 ; // no of wrappers needs to be    // exchanged for one chocolate.     cout << countMaxChoco(money, price, wrap);    return 0;}
// Efficient Java program to find maximum// number of chocolatesimport java.io.*; class GFG {     // Returns maximum number of chocolates    // we can eat with given money, price    // of chocolate and number of wrapprices    // required to get a chocolate.    static int countMaxChoco(int money,                        int price, int wrap)    {                 // Corner case        if (money < price)            return 0;             // First find number of chocolates        // that can be purchased with the        // given amount        int choc = money / price;             // Now just add number of chocolates        // with the chocolates gained by        // wrapprices        choc = choc + (choc - 1) / (wrap - 1);        return choc;    }         // Driver code    public static void main (String[] args)    {                 // total money        int money = 15;                 // cost of each candy        int price = 1;                 // no of wrappers needs to be        int wrap = 3 ;                 // exchanged for one chocolate.        System.out.println(           countMaxChoco(money, price, wrap));    }} // This code is contributed by anuj_67.
# Efficient Python3 program to find# maximum number of chocolates # Returns maximum number of# chocolates we can eat with# given money, price of chocolate# and number of wrapprices# required to get a chocolate.def countMaxChoco(money, price, wrap) :     # Corner case    if (money < price) :        return 0     # First find number of chocolates    # that can be purchased with the    # given amount    choc = int(money / price)     # Now just add number of    # chocolates with the chocolates    # gained by wrapprices    choc = choc + (choc - 1) / (wrap - 1)    return int(choc ) # Driver codemoney = 15 # total moneyprice = 1 # cost of each candywrap = 3 # no of wrappers needs to be         # exchanged for one chocolate. print(countMaxChoco(money, price, wrap)) # This code is contributed by Smitha
// Efficient C# program to find maximum// number of chocolatesusing System; class GFG {     // Returns maximum number of chocolates    // we can eat with given money, price    // of chocolate and number of wrapprices    // required to get a chocolate.    static int countMaxChoco(int money,                        int price, int wrap)    {                 // Corner case        if (money < price)            return 0;             // First find number of chocolates        // that can be purchased with the        // given amount        int choc = money / price;             // Now just add number of chocolates        // with the chocolates gained by        // wrapprices        choc = choc + (choc - 1) / (wrap - 1);        return choc;    }         // Driver code    public static void Main ()    {                 // total money        int money = 15;                 // cost of each candy        int price = 1;                 // no of wrappers needs to be        int wrap = 3 ;                 // exchanged for one chocolate.        Console.WriteLine(        countMaxChoco(money, price, wrap));    }} // This code is contributed by anuj_67.
<?php// Efficient PHP program to find maximum// number of chocolates // Returns maximum number// of chocolates we can eat// with given money, price// of chocolate and number// of wrapprices required// to get a chocolate.function countMaxChoco($money, $price, $wrap){    // Corner case    if ($money < $price)    return 0;     // First find number    // of chocolates that    // can be purchased    // with the given amount    $choc = $money / $price;     // Now just add number    // of chocolates with the    // chocolates gained by wrapprices    $choc = $choc + ($choc - 1) /                    ($wrap - 1);    return $choc;}     // Driver code          // total money    $money = 15 ;         // cost of each candy    $price = 1;         // no of wrappers needs to be    // exchanged for one chocolate.    $wrap = 3 ;    echo countMaxChoco($money, $price, $wrap);     // This code is contributed by ajit?>
<script> // Efficient Javascript program to find maximum// number of chocolates // Returns maximum number of chocolates// we can eat with given money, price// of chocolate and number of wrapprices// required to get a chocolate.function countMaxChoco(money, price, wrap){          // Corner case    if (money < price)        return 0;      // First find number of chocolates    // that can be purchased with the    // given amount    let choc = parseInt(money / price, 10);      // Now just add number of chocolates    // with the chocolates gained by    // wrapprices    choc = choc + parseInt((choc - 1) /                           (wrap - 1), 10);    return choc;} // Driver code // Total moneylet money = 15; // Cost of each candylet price = 1; // No of wrappers needs to belet wrap = 3; // Exchanged for one chocolate.document.write(countMaxChoco(money, price, wrap)); // This code is contributed by divyesh072019 </script>
Output : 
22
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
vt_m
Smitha Dinesh Semwal
Mithun Kumar
tusharsinghal6
chinmoy1997pal
divyesh072019
akshaysingh98088
Mathematical
Recursion
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Find minimum number of coins that make a given value
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction
Print all subsequences of a string