You are on page 1of 8

PROGRAMING AND DATA STRUCTURE

ASSIGNMENT -:
Topic - CHOCOLATE WRAPPER PROBLEM

SUBMITTED TO -
BIJOY CHAND CHATTERJEE

NAME -SHIVAM PANDEY


COURSE- MSC 1ST YEAR
ENROLLMENT NO. - SAU/CS(M)/2021/17
MAIL ID – 6shivam98@gmail.com
Aim -
🍫🍫🍫🍫🍫
To calculate how much chocolates a person can get with a
certain sum of money if he gets some more chocolates on
returning the wrappers of the chocolates.
The following three values will be given.
- Money - money you have to buy chocolates.
- Price – price of a one chocolate.
- Wrappers – no. of wrappers to be returned to get one
extra chocolate.
For e.g. -
Let say you have
- 20Rs
- Cost of 1 chocolate is 2
- And for every 3 wrappers you will get 1 extra
chocolate.
Now the total no. Of chocolate one can get with above
data is -
20/2 = 10 + 3 chocolates (availed with 9 wrappers) + 1
chocolate (availed with 3 wrappers and 1 wrapper left).
So, here if we count we got total 14 chocolates.
Approach to implement above puzzle -

First of all value should be positive and greater than one.


- 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.

- 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 analysing the two
conditions and stops until one them fails .
The conditions are - if(money==0 || money < price)
&& if(curr_wrap==0 || curr_wrap < wrap).

Below is algorith using above approach.


Algorithm for the above problem: -

1. Start

2. Definea function and declare (money,price,wraps)


0 variables in it to take input from the user.

3. Check for condition I.e. if(money==0 || money < price)


&& if(curr_wrap==0 || curr_wrap < wrap).

4. Declare a variable choco and initialise it with


money/price .

5. Declare a function that will take value of inputs


from the main() and calls functions until inside it.

6. Main() will call the function defined above and gives


ouput using it

7. End
Code for the above algorithm: -
#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 code
int main()
{
int money ; // total money
int price ; // cost of each candy
int wrap ; // no of wrappers needs to be
cout << "enter the values for money , price
,wraps\n";
cin >> money>>price>>wrap;
// exchanged for one chocolate.

cout << countMaxChoco(money, price, wrap);


return 0;
}

Time complexity -:
The time complexity of a given solution O(m) ..where m
is the no. Of iterations.
Output -

*a exe file would be attached with this pdf

You might also like