Combinations
Combinations
AfterAcademy
Admin AfterAcademy
8 Aug 2020
Difficulty: Medium
https://afteracademy.com/blog/combinations/ 1/9
1/2/25, 12:17 PM Combinations
Problem Note
Example 1
Input: n = 3, k = 2
Output:
[
[1,2],
[1,3],
[2,3]
]
Example 2
Input: n = 5, k = 3
Output:
[
[1 2 3],
[1 2 4],
[1 2 5],
[1 3 4],
[1 3 5],
[1 4 5],
[2 3 4],
[2 3 5],
[2 4 5],
[3 4 5]
]
https://afteracademy.com/blog/combinations/ 2/9
1/2/25, 12:17 PM Combinations
Solution
This problem is a famous backtracking problem.
You might have understood what’s happening while looking over the
examples. Let’s take an example, given k is 2, that means we have to
generate combinations of length 2 and the set of numbers we have are from
1 to n, in this case, n is 4. We will solve this problem by using a backtracking
approach.
N = 4, K = 2.
backtrack(1, [])
backtrack(2, [1])
backtrack(3, [1, 2])
backtrack(4, [1, 3])
backtrack(5, [1, 4])
backtrack(3, [2])
backtrack(4, [2, 3])
backtrack(5, [2, 4])
backtrack(4, [3])
backtrack(5, [3, 4])
backtrack(5, [4])
If you will look at the above stack, you’ll see how the functions can be
calling itself until the current combination array length becomes equal
to k . As soon as it touches the length k , it starts to backtrack to the
previous state. If there are not enough elements in the current
combination , it will run through fisrt~N and append all the element num
to the combination , and set the first to num+1 .
In other words, if we have used 3, we will not use 3 and the number below 3
anymore.
https://afteracademy.com/blog/combinations/ 3/9
1/2/25, 12:17 PM Combinations
Solution Steps
Pass a parameter start in the backtrack function which will tell the
backtrack function from where to look for the elements.
}
for(int i=start to i<=n) {
curr_comb.add(i)
backtrack(combs, curr_comb, i+1, n, k-1)
curr_comb.remove(curr_comb.size() - 1)
}
}
https://afteracademy.com/blog/combinations/ 5/9
1/2/25, 12:17 PM Combinations
Complexity Analysis
Do you think that the time complexity for iterative and recursive
approaches would be the same?
Can you draw a recursive tree diagram following the recursive approach
for Example 2 mentioned above?
In curr_comb.remove(curr_comb.size() — 1) Why do we
remove the last one?
Considering Example 1, if you want to get [1,3] after you had got [1,2], there
is no need to create another list, you just need to remove ‘2’— that is the
curr_comb.size()-1 .
Permutations
Sudoku Solver
Rat in a maze
https://afteracademy.com/blog/combinations/ 6/9
1/2/25, 12:17 PM Combinations
If you have any more approaches or you find an error/bug in the above
solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
https://afteracademy.com/blog/combinations/ 7/9
1/2/25, 12:17 PM Combinations
Generate Parentheses
Given n pairs of parentheses, write a
program to generate all combinations of
balanced parentheses. You have to return a
string array containing all possible cases.
Admin AfterAcademy
18 Jul 2020
https://afteracademy.com/blog/combinations/ 8/9
1/2/25, 12:17 PM Combinations
https://afteracademy.com/blog/combinations/ 9/9