Program for Chocolate and Wrapper Puzzle
Last Updated :
07 Feb, 2023
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 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++
#include <iostream>
using namespace std;
int countRec( int choc, int wrap)
{
if (choc < wrap)
return 0;
int newChoc = choc/wrap;
return newChoc + countRec(newChoc + choc%wrap,
wrap);
}
int countMaxChoco( int money, int price, int wrap)
{
int choc = money/price;
return choc + countRec(choc, wrap);
}
int main()
{
int money = 15 ;
int price = 1;
int wrap = 3 ;
cout << countMaxChoco(money, price, wrap);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countRec( int choc, int wrap)
{
if (choc < wrap)
return 0 ;
int newChoc = choc / wrap;
return newChoc + countRec(newChoc +
choc % wrap, wrap);
}
static int countMaxChoco( int money,
int price, int wrap)
{
int choc = money/price;
return choc + countRec(choc, wrap);
}
public static void main (String[] args)
{
int money = 15 ;
int price = 1 ;
int wrap = 3 ;
System.out.println(
countMaxChoco(money, price, wrap));
}
}
|
Python3
import math
def countRec(choc, wrap):
if (choc < wrap):
return 0 ;
newChoc = choc / wrap;
return newChoc + countRec(newChoc + choc %
wrap, wrap);
def countMaxChoco(money, price, wrap):
choc = money / price;
return math.floor(choc + countRec(choc, wrap));
money = 15 ;
price = 1 ;
wrap = 3 ;
print (countMaxChoco(money, price, wrap));
|
C#
using System;
class GFG {
static int countRec( int choc, int wrap)
{
if (choc < wrap)
return 0;
int newChoc = choc / wrap;
return newChoc + countRec(newChoc +
choc % wrap, wrap);
}
static int countMaxChoco( int money,
int price, int wrap)
{
int choc = money/price;
return choc + countRec(choc, wrap);
}
public static void Main ()
{
int money = 15 ;
int price = 1;
int wrap = 3 ;
Console.WriteLine(
countMaxChoco(money, price, wrap));
}
}
|
PHP
<?php
function countRec( $choc , $wrap )
{
if ( $choc < $wrap )
return 0;
$newChoc = $choc / $wrap ;
return $newChoc + countRec( $newChoc + $choc % $wrap ,
$wrap );
}
function countMaxChoco( $money , $price , $wrap )
{
$choc = $money / $price ;
return floor ( $choc + countRec( $choc , $wrap ));
}
$money = 15;
$price = 1;
$wrap = 3 ;
echo countMaxChoco( $money , $price , $wrap );
?>
|
Javascript
<script>
function countRec(choc, wrap)
{
if (choc < wrap)
return 0;
let newChoc = choc / wrap;
return newChoc + countRec(newChoc +
choc % wrap, wrap);
}
function countMaxChoco(money,
price, wrap)
{
let choc = money/price;
return choc + countRec(choc, wrap);
}
let money = 15 ;
let price = 1;
let wrap = 3 ;
document.write(Math.floor(
countMaxChoco(money, price, wrap)));
</script>
|
Output :
22
Time complexity: O(log n)
Space complexity: O(n), because of recursion stack space.
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++
#include <iostream>
using namespace std;
int countMaxChoco( int money, int price, int wrap)
{
if (money < price)
return 0;
int choc = money / price;
choc = choc + (choc - 1) / (wrap - 1);
return choc;
}
int main()
{
int money = 15 ;
int price = 1;
int wrap = 3 ;
cout << countMaxChoco(money, price, wrap);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countMaxChoco( int money,
int price, int wrap)
{
if (money < price)
return 0 ;
int choc = money / price;
choc = choc + (choc - 1 ) / (wrap - 1 );
return choc;
}
public static void main (String[] args)
{
int money = 15 ;
int price = 1 ;
int wrap = 3 ;
System.out.println(
countMaxChoco(money, price, wrap));
}
}
|
Python3
def countMaxChoco(money, price, wrap) :
if (money < price) :
return 0
choc = int (money / price)
choc = choc + (choc - 1 ) / (wrap - 1 )
return int (choc )
money = 15
price = 1
wrap = 3
print (countMaxChoco(money, price, wrap))
|
C#
using System;
class GFG {
static int countMaxChoco( int money,
int price, int wrap)
{
if (money < price)
return 0;
int choc = money / price;
choc = choc + (choc - 1) / (wrap - 1);
return choc;
}
public static void Main ()
{
int money = 15;
int price = 1;
int wrap = 3 ;
Console.WriteLine(
countMaxChoco(money, price, wrap));
}
}
|
PHP
<?php
function countMaxChoco( $money , $price , $wrap )
{
if ( $money < $price )
return 0;
$choc = $money / $price ;
$choc = $choc + ( $choc - 1) /
( $wrap - 1);
return $choc ;
}
$money = 15 ;
$price = 1;
$wrap = 3 ;
echo countMaxChoco( $money , $price , $wrap );
?>
|
Javascript
<script>
function countMaxChoco(money, price, wrap)
{
if (money < price)
return 0;
let choc = parseInt(money / price, 10);
choc = choc + parseInt((choc - 1) /
(wrap - 1), 10);
return choc;
}
let money = 15;
let price = 1;
let wrap = 3;
document.write(countMaxChoco(money, price, wrap));
</script>
|
Output :
22
Time Complexity : O(1) ,as we are not using any looping statements in our program.
Space Complexity : O(1) ,as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...