Combinations
Combinations is a medium recursion & backtracking problem solved with the pick / not-pick pattern.
The best approach, optimal (loop with pruning), runs in O(k · C(n, k)) time and O(k) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from pick / not-pick over 1..n up.
Problem
Given n and k, return all combinations of k numbers chosen from 1..n, in any order. [1,2] and [2,1] are the same combination.
Examples
Example 1
- Input
n = 4, k = 2- Output
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Example 2
- Input
n = 3, k = 3- Output
[[1,2,3]]
Constraints
1 <= k <= n <= 20
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Pick / not-pick over 1..n | O(k · C(n, k)) | O(n) |
| Optimal (loop with pruning) | O(k · C(n, k)) | O(k) |
1Pick / not-pick over 1..n
O(k · C(n, k))O(n)Recursion depth up to n.For each number decide to take it or skip it, and record when the path has k numbers.
- If path.size() == k, record.
- If i > n, return.
- Take i and recurse; skip i and recurse.
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> out = new ArrayList<>();
pick(1, n, k, new ArrayList<>(), out);
return out;
}
private void pick(int i, int n, int k, List<Integer> path, List<List<Integer>> out) {
if (path.size() == k) { out.add(new ArrayList<>(path)); return; }
if (i > n) return;
path.add(i);
pick(i + 1, n, k, path, out);
path.remove(path.size() - 1);
pick(i + 1, n, k, path, out);
}
}class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
out: List[List[int]] = []
self.pick(1, n, k, [], out)
return out
def pick(self, i: int, n: int, k: int, path: List[int], out: List[List[int]]) -> None:
if len(path) == k:
out.append(path[:])
return
if i > n:
return
path.append(i)
self.pick(i + 1, n, k, path, out)
path.pop()
self.pick(i + 1, n, k, path, out)class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> out;
vector<int> path;
pick(1, n, k, path, out);
return out;
}
private:
void pick(int i, int n, int k, vector<int>& path, vector<vector<int>>& out) {
if ((int)path.size() == k) { out.push_back(path); return; }
if (i > n) return;
path.push_back(i);
pick(i + 1, n, k, path, out);
path.pop_back();
pick(i + 1, n, k, path, out);
}
};function combine(n, k) {
const out = [];
pick(1, n, k, [], out);
return out;
}
function pick(i, n, k, path, out) {
if (path.length === k) { out.push([...path]); return; }
if (i > n) return;
path.push(i);
pick(i + 1, n, k, path, out);
path.pop();
pick(i + 1, n, k, path, out);
}2Optimal (loop with pruning)
O(k · C(n, k))O(k)At each level loop over the next number from start. If fewer than k - path.size() numbers remain, no complete combination is possible, so stop the loop early.
- need = k - path.size(); if need == 0, record.
- for i from start to n - need + 1: add i, recurse with i + 1, remove.
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> out = new ArrayList<>();
build(1, n, k, new ArrayList<>(), out);
return out;
}
private void build(int start, int n, int k, List<Integer> path, List<List<Integer>> out) {
int need = k - path.size();
if (need == 0) { out.add(new ArrayList<>(path)); return; }
for (int i = start; i <= n - need + 1; i++) {
path.add(i);
build(i + 1, n, k, path, out);
path.remove(path.size() - 1);
}
}
}class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
out: List[List[int]] = []
self.build(1, n, k, [], out)
return out
def build(self, start: int, n: int, k: int, path: List[int], out: List[List[int]]) -> None:
need = k - len(path)
if need == 0:
out.append(path[:])
return
for i in range(start, n - need + 2):
path.append(i)
self.build(i + 1, n, k, path, out)
path.pop()class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> out;
vector<int> path;
build(1, n, k, path, out);
return out;
}
private:
void build(int start, int n, int k, vector<int>& path, vector<vector<int>>& out) {
int need = k - (int)path.size();
if (need == 0) { out.push_back(path); return; }
for (int i = start; i <= n - need + 1; i++) {
path.push_back(i);
build(i + 1, n, k, path, out);
path.pop_back();
}
}
};function combine(n, k) {
const out = [];
build(1, n, k, [], out);
return out;
}
function build(start, n, k, path, out) {
const need = k - path.length;
if (need === 0) { out.push([...path]); return; }
for (let i = start; i <= n - need + 1; i++) {
path.push(i);
build(i + 1, n, k, path, out);
path.pop();
}
}Edge cases to test
- k = n (one combination)
- k = 1
Hints
Hint 1
Only choose numbers larger than the last one so each set appears once, and stop early when too few numbers are left to reach k.
FAQ
What is the best time complexity for Combinations?
Optimal (loop with pruning) runs in O(k · C(n, k)) time and O(k) extra space.
Which pattern does Combinations use?
It is a recursion & backtracking problem that uses the pick / not-pick pattern. Other problems with the same pattern: Generate all binary strings without consecutive 1's, Subsets (2 choices per element), Subsets II.
Is there a brute force solution for Combinations?
Yes. Pick / not-pick over 1..n takes O(k · C(n, k)) time and O(n) space. For each number decide to take it or skip it, and record when the path has k numbers.
Which edge cases should I test for Combinations?
k = n (one combination); k = 1.