77. Combinations
Combinations (LeetCode 77) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Backtracking Patterns pattern, in the Subsets (Include/Exclude) subpattern.
How to solve Combinations
Combinations is a Generate Subsets problem, part of Backtracking Patterns. Generate all subsets using include/exclude decision pattern. Handle duplicates in subsets.
Work towards the solution in these steps:
- Consider using backtracking to explore all possible combinations systematically.
- At each step, decide whether to include the current number in the combination or skip it, following the include/exclude pattern.
- Maintain a temporary list to store the current combination and add it to the results once its size reaches k.
The reference solution runs in O(C(n, k) * k) time and O(C(n, k) * k) space.
The Combinations solution and editorial walks through the full approach with reference code.
Problem statement
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].
You may return the answer in any order.
Example 1:
Input: n = 4, k = 2 Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] Explanation: There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Example 2:
Input: n = 1, k = 1 Output: [[1]] Explanation: There is 1 choose 1 = 1 total combination.
Constraints:
- 1 <= n <= 20
- 1 <= k <= n
Topics and companies
Topics: Backtracking.
Reported in interviews at Facebook, Amazon.
How to practise Combinations on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(C(n, k) * k) time and O(C(n, k) * k) space.
Open Combinations in the code editor, or read the Combinations editorial for a worked solution with its approach and complexity analysis.
Where Combinations sits in the DSA pattern sheet
Problems related to Combinations
Other problems that use the same Subsets (Include/Exclude) and Backtracking Patterns techniques:
- Letter Combinations of a Phone Number — Medium, same subpattern
- Subsets — Medium, same subpattern
- Subsets II — Medium, same subpattern
- Sum of All Subset XOR Totals — Easy, same subpattern
- Check if Word Can Be Placed In Crossword — Medium, same pattern
- Combination Sum — Medium, same pattern
Combinations FAQ
What is the LeetCode number of Combinations?
Combinations is LeetCode problem 77, rated Medium.
What pattern is used to solve Combinations?
Combinations is a Generate Subsets problem. It belongs to the Backtracking Patterns pattern, in the Subsets (Include/Exclude) subpattern.
What is the time complexity of Combinations?
The reference solution runs in O(C(n, k) * k) time and O(C(n, k) * k) space.
Which companies ask Combinations?
Combinations has been reported in interviews at Facebook, Amazon.