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.
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:
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:
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:
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:
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
- Counting subsets with given sizes of a set
- Counting the adjacent swaps required to convert one permutation into another
- Create 2d triangles from 2d points
- Create a random permutation of 1..N in constant space
- Create non-intersecting polygon passing through all given points
- Creating all strongly connected graphs with given in-degree with equal probability
- Cutting Stock Problem
- Cycle of maximum weight in a graph

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