Home/Blog/Programming/The art of counting: permutations and combinations
Home/Blog/Programming/The art of counting: permutations and combinations

The art of counting: permutations and combinations

17 min read
Jan 22, 2024
content
Doing it the right way
Permutation vs. combination
Commonly encountered counting problems
A basic counting principle
Counting permutations
Counting permutations of a subset
There might be other scenarios
Practice makes perfect
How useful is all this?

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

Doing it the right way#

Formulas are usually easier to remember once they are understood from first principles. When recalling the difference between the formulas for permutations and combinations, the act of remembering can essentially be a split-second process of rediscovery—as long as a few concepts are understood properly.

In this blog, we cover the concepts of permutations and combinations, and show how the formulas to count permutations and combinations arise from a basic counting principle. We include some basic examples and some harder curve balls to help reinforce fundamental concepts.

Permutation vs. combination#

A combination is a collection of items that have no prescribed order. So a bag of things, a group of people, and a school of fish are all examples of a combination.

A permutation is a sequence of items with some specific order imposed on those items. If there’s a group of people standing in a queue, there’s a first person, a second person, and so on. That arrangement of individuals is an example of a permutation.

As evident from these definitions, the difference between a combination and a permutation is that a combination refers to an unordered set, whereas a permutation refers to an ordered linear arrangement of the elements of a set.

The concepts of combinations and permutations often arise in counting scenarios where we are interested in counting the number of permutations or combinations of a particular size. There are two convenient expressions that emphasize the size of a combination or a permutation: A combination consisting of kk elements (chosen from a possibly bigger set) is concisely referred to as a kk-combination. Similarly, a permutation consisting of kk elements (chosen from a possibly bigger set) is called a kk-permutation.

A 9-combination of fish when no order is specified
A 9-combination of fish when no order is specified
A 5-permutation of individuals lined up in a queue
A 5-permutation of individuals lined up in a queue

Commonly encountered counting problems#

A typical question we need to ask ourselves when solving a problem might look like one of the following:

  • Counting kk-permutations: We’ve got nn elements. How many ways are there to arrange kk of these in a linear order?

  • Counting kk-combinations: We’ve got nn elements. How many different ways are there to choose any kk of these?

Answering these is as easy as plugging the numbers into two formulas, but it’s far more useful in the long run to absorb the thought process that goes into arriving at those formulas. That’s because the thought process is immediately helpful for arriving at solutions to a wider range of problems. We start with a fundamental principle of counting.

A basic counting principle#

To complete a task consisting of two jobs, if there are mm ways to do the first job and nn ways to do the second, there are m×nm\times n ways to finish the task.

If there are 3 ways to pick a circle and 2 ways to pick a triangle, then there are 6 ways to pair a circle and a triangle.
If there are 3 ways to pick a circle and 2 ways to pick a triangle, then there are 6 ways to pair a circle and a triangle.

We use the following visual cue to help recall this counting principle:

We think about this as: if there are mm choices for the first decision, and nn choices for the second, then there are m×nm\times n ways to get the whole task done.

Example 1: If there are 2 different kinds of bread and 3 different kinds of spreads, how many ways are there to make a sandwich?

Since for each type of bread, we can use any of the 3 spreads, there are 6 different ways to make a sandwich.

There are 2 x 3 = 6 ways to make a sandwich
There are 2 x 3 = 6 ways to make a sandwich

Example 2: Suppose we want to decorate a card using glitter and a sticker. There are 3 kinds of paper, 2 kinds of glitter, and 5 different stickers to choose from. In how many ways can we make the card?

There are 3 x 2 x 5 = 30 ways to make a card
There are 3 x 2 x 5 = 30 ways to make a card

Since we can try all variations of a paper against the different types of glitter, and then for each of those 3×2=63 \times 2 = 6 paper-and-glitter pairings, we can use any of the 5 stickers, therefore, there are 6×5=306 \times 5 = 30 ways altogether.

Counting permutations and combinations are direct applications of this basic counting principle.

Counting permutations#

If there are nn objects, and we have to line them up, then there are nn choices for the item in the first place, n1n-1 choices for the item in the second place, and so on.

Number of possibilities for each of the n places
Number of possibilities for each of the n places

So there are n×(n1)××1=n!n\times (n-1)\times \cdots \times 1 = n! permutations.

Example: Let’s think about the number of ways to permute the animals shown below.

There are 4!4! permutations since that’s the number of ways to arrange the four animals.

Now let’s apply the same counting principle to count kk-permutations on a set of nn elements.

Counting permutations of a subset#

If kk objects from a set of nn objects are to be arranged in a linear order, there are nn objects that can go in the first place. Once an object goes in the first place, there are n1n-1 choices for the second place, n2n-2 for the third place, and n(k1)n-(k-1) choices for the kthk^{th} place.

Number of possibilities for each of the k places
Number of possibilities for each of the k places

So the total number of ways is

n×(n1)×(n2)××(n(k1))n \times (n-1) \times (n-2) \times \cdots \times (n- (k-1))

This is the number of kk-permutations from a set of size nn, but its not too easy on the eyes! Fortunately, we can make it more presentable, since the value of the expression remains unchanged when it’s multiplied and divided by the same quantity:

n××(nk+1)=n××(nk+1)(nk)!(nk)!=n!(nk)!\begin{align*} n \times \cdots \times (n-k+1) &= \frac{n \times \cdots \times (n-k+1)}{(n-k)!}(n-k)!\\ &= \frac{n!}{(n-k)!} \end{align*}

And voilà, this neat and tidy formula is often expressed through the notation P(n,k)P(n, k):

P(n,k)=n!(nk)!\begin{align*} P(n,k) &= \frac{n!}{(n-k)!} \end{align*}

Tip: In case of difficulty recalling this formula, we go back to square one by applying the basic counting principle and rediscovering it from scratch.

1.

Given a box of 12 different chocolates, how many ways are there to eat all the chocolates, if 1 chocolate is consumed every day?

Show Answer
Q1 / Q2

A similar notation for representing the number of ways to choose a kk-combination from a group of nn elements is denoted C(n,k)C(n,k). This notation is read out loud as n``n choose kk."" Just verbalizing it this way makes it easy to remember what it represents.

As each kk-combination is a set of kk elements, it can be ordered in k!k! different ways to get different kk-permutations.

A 3-combination corresponds to 3!=6 3-permutations
A 3-combination corresponds to 3!=6 3-permutations

This can be expressed as:

C(n,k)Number ofk-combinations×k!Number of ways to permute eachk-combination=P(n,k)Number ofk-permutations\begin{align*} \underbrace{C(n,k)}_{\substack{\text{Number of}\\ \text{k-combinations}}} \times \underbrace{k!}_{\substack{\text{Number of ways to}\\ \text{ permute each} \\ \text{k-combination}} } &= \underbrace{P(n, k)}_{\substack{\text{Number of}\\ \text{k-permutations}}} \end{align*}

    C(n,k)×k!=n!(nk)!    C(n,k)=n!k!(nk)!\begin{align*} \implies C(n,k)\times k! &= \frac{n!}{(n-k)!}\\ \implies \qquad C(n,k) &=\frac{n!}{k! (n-k)!} \end{align*}

An alternate notation for C(n,k)C(n,k) is (nk)\binom{n}{k}, and this is our preferred choice of notation. Note that (nk)\binom{n}{k} is commonly known as a binomial coefficient.

Takeaway: The formula for counting kk-combinations from a set of nn elements is:

(nk)=n!k!(nk)!\binom{n}{k}=\frac{n!}{k!(n-k)!}

1.

We’ve got 10 different pieces of fruits in our grocery bag, and we pick 2 to give to a child. How many ways are there to do this?

Show Answer
Q1 / Q2

There might be other scenarios#

Having covered the basics, we should not be lulled into a false sense of security. In order to count effectively, we need to be aware of when not to apply these formulas.

One of the pitfalls to watch out for is a scenario where we are counting sequences of elements drawn from a set, such that each element can have a repeat occurrence within a sequence. Such sequences are called permutations with repetitions.

Example: Suppose we want to count bit strings of length 6 such that each character in the string is a 0 or a 1.

Since we are allowed to reuse the two characters (0 or 1), there are two choices for the first position, two choices for the second position, two choices for the third position, and so on. So the number of such bit strings is:

2×2××2For 6 positions=26\underbrace{2 \times 2 \times \cdots \times 2}_{\text{For }6\ \text{positions}} = 2^6

Notice how we are able to arrive at the solution for the above problem by applying a simple counting argument. In general, if there are nn elements, the number of permutations of size kk with allowed repetitions is nkn^k.

n×n××n For k places=nk\begin{align*} \underbrace{n \times n \times \cdots \times n}_{\ \text{For } k \text{ places}} = n^k \end{align*}

There might be other situations that require more fine-tuning. For now, we want to stop and consolidate what we’ve learned so far.

Practice makes perfect#

Let’s get more practice thinking about a mixed variety of counting problems!

Counting problems of a mixed variety

1.

Each player’s field position in a volleyball team has a specific strategic role. How many ways are there to pick a volleyball team of size 6 from 25 athletes, such that each person is assigned a specific role?

Show Answer
Q1 / Q7

How useful is all this?#

Knowing the difference between permutations and combinations is, without exaggeration, essential knowledge for most engineering disciplines. Here are some of the ways in which the concepts explored in this blog are applied in different kinds of content on the Educative platform:

  • The Normal and Binomial Distribution lesson in a data science course employs these ideas to calculate probabilities.

  • The running time for the code in the Solution: Permutations lesson of a course on coding interviews was calculated by realizing that there are P(n,k)P(n,k) nodes in the recursion tree corresponding to the recursive code.

  • The Subsets: Introduction lesson talks about a general pattern—seen in coding interviews—that pertains to finding combinations and permutations in different data structures.

  • The size of the search space for a brute force algorithm referred to in the approximation algorithms blog was estimated using the ideas covered in this blog.

  • The One-Time Pads lesson in a course on cryptography contains an example of counting permutations with allowed repetitions.

There is a lot more fun to be had with the kind of combinatorial reasoningThis refers to reasoning pertaining to combinatorics, which—broadly speaking—is the branch of mathematics that’s concerned with discrete structures with a special emphasis on counting. seen here. But that’s a topic for a different day. We hope this was interesting for you, and that you learned something new today!

Frequently Asked Questions

How to do permutations and combinations?

In permutations, the sequence in which objects are arranged is significant. This means that altering the order of chosen objects leads to distinct permutations. Conversely, in combinations, the arrangement sequence is irrelevant, so various configurations of the same group of objects are treated as identical.

What is the difference between permutations and combinations?


Written By:
Mehvish Poshni
Join 2.5 million developers at
Explore the catalog

Free Resources