Skip to main content

Combinations

This page provides links to solutions that use the combinations.

Overview

Combinations represent selections of items from larger group, where the order doesn't matter. The formula for calculating combinations is as below:

nCr=n!r!(nr)!^\text{n}\text{C}_\text{r}= \frac{\text{n}!}{\text{r}!(\text{n}−\text{r})!}

Where n\text{n} is the total number of items, and r\text{r} is the number of items to choose.

How to Spot These Problems

You can identify combination problems if the problem requires you to:

  • Choose a subset of items from a larger set, to make K\text{K} subsets.
info

All the combinations problem can also be solved using backtracking approach as well.

Think of it this way: instead of choosing a subset of item from larger set to make K\text{K} subsets, we can assign each element of set to K\text{K} subsets. However, if we use backtracking, time complexity of the problem becomes KN\text{K}^{\text{N}}. If K\text{K} is too large, this would lead to time limit exceed exception.

To avoid this, we use combinatorial approach, where we first generate single subset from larger set which takes time complexity of 2N2^\text{N}. Given that we need to generate K\text{K} such subsets, the total time complexity becomes K2N\text{K} * 2^\text{N}.

Leetcode Problem Set

# Solution
698Partition to K Equal Sum Subsets
Total Solved: 1