Skip to main content

Chocolate in the Boxes

Image for post

Counting problems have been irritating people for ages. They are generally long and weird with additions — substractions going on in different parts with meanings difficult to understand.
Out of all the popular counting problems, one can be solved using some chocolates and boxes, which I am going to discuss in this article.

The Problem

Image for post

For the given equation, find the number of ways such that a, b, c, d has a whole number solution, i.e. a, b, c, d ≥ 0.

Most of you might know the formula for the problem, here we are going to discuss a thought on how to tackle such a problem and formulate it as people find it tough to memorize and often forget. Before discussing further let’s see what the chocolate problem is.

The Chocolate Problem
Let’s say, we have 10 chocolates, that we need to put in 4 different boxes. It is assumed that all the chocolates are identical, and we are asked to count the number of ways to put the chocolates into the boxes. Putting no chocolates at all into the box is totally acceptable.

Approach
To solve this problem we will put all the chocolates onto a table in a line and use three identical separators or ‘verticles’ with them.

Image for post
10 chocolates separated by 3 verticles

The verticles separate the chocolates into different sections. Chocolates that lie between two verticles can go into a box.
For our problem let us assume that the chocolates to the left of the first verticle can go into the first box, those which are lying between the first and second verticle will go into the second box and similarly, chocolates lying between second and last verticle will go into box number three and the rightmost bunch of chocolates goes into the fourth box.

From here we can say that the number of ways to arrange these 10 chocolate and 3 verticles can give us the solution to the problem.
But wait, will all arrangements be valid.

Yes, with a simple demonstration we can show that all the arrangements generated are valid for solving the problem. Some of the weird arrangements are shown below. Remember that the boxes can be left empty.

Image for post
Arrangement 1: Distribution of chocolates is {2, 0, 5, 3}

In Arrangement 1, the first and second verticle come side by side. Hence it represents the case when no chocolate goes into the second box. Hence the second box remains empty.

Image for post
Arrangement 2: Distribution of chocolates {0, 4, 6, 0}

In Arrangement 2, the first and fourth box remains empty, as there are no chocolates left on another side of the first and last verticle.

Image for post
Arrangement 3: Distribution of chocolates {10, 0 , 0 ,0 }

Similarly, in Arrangement 3, all the 10 chocolates go into the first box, and all the other three boxes go empty.

Now the question is reduced to count the number of ways to arrange the things lying on the table.
The formula to count the number of ways to arrange N things is ‘N!’
And the formula to count the number of ways to arrange N, things out of which R are identical is (N!/R!).

Now for our problem, we have assumed in the beginning that the chocolates and verticles are identical to their respective kind. Hence the number of ways to arrange these 10 identical chocolates + 3 identical verticles is

Image for post

The above formula can also be represented as

Image for post

Hence the result is the answer to the given chocolate problem.

Suppose in a more general case we had n chocolates and boxes then, to solve the problem we would have used k -1 separators. Then we would have n+k-1 total objects on the table. Hence the solution to this general case would have been

Image for post

Observation
If we compare our original problem to the chocolate problem, we can use each of our variable (i.e. a, b, c, d) as one box and let the total number of chocolates be 100, then using the same technique we can solve the problem of finding all possible ways to assign a whole numbers to each variable. That’s it.

So rather than memorising a scary formula without meaning, it’s better to learn a technique to formulate it. This helps in two ways:

  • You cannot deduce the formula wrong if you imagine things in the right way. In case you have memorized it, then you can always verify it.
  • You can handle a lot of variation to the same problem easily, just by closing your eyes and taking examples.

And next time you meet a complex or irritating combinatorics formula, then you can say that there must be an interesting story behind it too, or you can write one of yours!

Signing off

Comments

Popular posts from this blog

CodeChef March Challenge 2021 — Consecutive Addition

“You are given two matrices A and B, each with R rows (numbered 1 through R) and C columns (numbered 1 through C). Let’s denote an element of A or B in row i and column j by A(i,j) or B(i,j) respectively….. ” You can read the whole problem here . Problem description in short Given two matrices, A and B. Task is to transform A to B using special some addition-based criteria. The criteria for transformation is as follows we are given a number K 2 ≤ K ≤ min(R, C), where R is row size and C is column size we have to take any submatrix of size Kx1 (i.e. along the column) or 1xK (i.e. along the row), in A, and to all the numbers that fall under that submatrix have, add some integer v, this can be chosen by us. This has to be done in such a way that finally A transforms to B. The actual problem is to check if this kind of transformation is possible, given A and B. Ideation…. Befor e  I begin I wanna say that it is an observation based problem, can have many solutions and my solution is not pe

Climbing the Fibonacci Sequence

  Climbing Staircase Problem T he Story begins with a Problem Yesterday, I was solving a very famous DP problem,  the climbing stairs . The problem is quite simple. You are climbing a staircase. It takes  n  steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Solution The solution is also pretty straight forward. Suppose we have climbed up some steps and we are left with some  i  steps to reach the top. Let’s say,  NoOfWays(i)  be a function that returns the number of ways to climb  i  steps to reach the top. If we climb by 1 step, then we are left with (i-1) steps. Using the above formula, the number of ways to climb (i -1) steps = NoOfWays(i-1). Similarly, if we climb by 2 steps, then we are left with (i-2) steps, and using the formula, the number of ways to climb (i -2) steps = NoOfWays(i-2). Since we can climb in one of the two fashions only so the number of ways to climb  i  steps is given as follows For the b