combinatorics
combinations
permutations
math efficiency
probability calculations

counting combinations and permutations efficiently

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Counting combinations and permutations efficiently is a fundamental task in the fields of mathematics and computer science, with applications ranging from probability theory to computer algorithms. Understanding the difference between permutations and combinations and knowing how to calculate them efficiently can significantly enhance problem-solving capabilities.

Permutations

Permutations refer to the arrangement of objects in a specific order. Unlike combinations, the order of selection matters in permutations.

Formula for Permutations

The number of permutations of selecting r objects from a set of n distinct objects is given by:

P(n,r)=n!(nr)!P(n, r) = \frac{n!}{(n - r)!}

Where n! (n factorial) represents the product of all positive integers up to n .

Example

Consider a set of three letters: A , B , C . If we want to find all permutations of choosing 2 out of the 3 letters, we calculate:

P(3,2)=3!(32)!=3×2×11=6P(3, 2) = \frac{3!}{(3-2)!} = \frac{3 \times 2 \times 1}{1} = 6

The permutations are: AB , BA , AC , CA , BC , CB .

Combinations

Combinations refer to the selection of objects where the order does not matter.

Formula for Combinations

The number of combinations of selecting r objects from a set of n distinct objects is given by:

C(n,r)=n!r!(nr)!C(n, r) = \frac{n!}{r!(n - r)!}

Example

Using the same set of letters, A , B , C , if we want to find all combinations of choosing 2 out of the 3 letters, we calculate:

C(3,2)=3!2!(32)!=3×2×12×1×1=3C(3, 2) = \frac{3!}{2!(3-2)!} = \frac{3 \times 2 \times 1}{2 \times 1 \times 1} = 3

The combinations are: AB , AC , BC .

Efficient Calculation in Programming

When dealing with large numbers in programming, it is essential to compute permutations and combinations efficiently to avoid overflow and excessive computation. Here are some methods to optimize these calculations:

Recursive Factorial Function

Using recursion, a factorial function can be computed efficiently up to a reasonable limit:

Probabilistic Calculations: Determining likelihoods in random experiments. • Algorithm Design: Solving problems like the traveling salesman problem. • Cryptography: Designing secure cryptographic keys based on combinatorial principles. • Combinatorial Design: Planning experiments and arranging schedules efficiently.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.