Shopkeeper sells half of his chocolates to each customer, giving him extra half if the number is odd

130 Views Asked by At

A shopkeeper has a carton of chocolates with him initially (the number of chocolates is not known to him). Each of the chocolate was priced at 18 rupees . If the shopkeeper has even number of chocolates he sells exactly half of the chocolates he has to the immediate next customer. If he has odd number of chocolates , he sells half of the chocolates he has at the above mentioned cost and gives an additional half chocolate free of cost to the customer (Assume that he sells half chocolate for half the price of a single chocolate) . He continues this process till he is left with no chocolates.

The shopkeeper records his transactions as 'Extra' if he has given some person an extra half chocolate and as 'Half' if he has given exactly half of the chocolates he had . His recordings in order are as follows (from the first customer): Extra , Half , Extra , Half , Half , Half , Extra . Calculate the number of chocolates the shopkeeper had initially.

And the answer is: We first need to find out how many chocolates the shopkeeper had initially. We construct a string with all zeroes and length equal to the number of customers . If the ith customer (from the end) was given an extra half chocolate , we replace the ith entry of string with 1 . One could observe that the resulting string we get is indeed the binary representation of the number of chocolates the shopkeeper had initially . Converting this binary representation to decimal number , we get the number of chocolates .

Regarding solving the problem with this approach - My question is how do we know that the number of chocolates is written backwards, and why do we white 1s when the shopkeeper has odd number of chocolates. I know I have weakness with binary numbers logic

2

There are 2 best solutions below

1
On BEST ANSWER

Forget about binary representation of numbers. Just consider the given sequence

Extra , Half , Extra , Half , Half , Half , Extra

and try to reconstruct what happened by rewinding the story from its end. Then write all the numbers you obtain in binary and find the reigning pattern.

0
On

Before the last transaction of the day he must have $1$ chocolate because if he has any more he won't run out. That will be an Extra at the end of the list. To get to $1$ he has to start with $2$ and do Half or $3$ and do Extra. If we make a correspondence between Extra and $1$ we can make a table just for these $$\begin {array}{r r r} starting number&writes&number\\ 1&Extra&1\\2&Half,Extra&01\\3&Extra,Extra&11\\4&Half,Half,Extra&001 \end {array}$$ and you should be able to see the pattern.