![]() |
VOOZH | about |
You are given an array arr[] consisting of n elements. Your task is to generate and print all possible combinations of exactly r elements from this array.
Note: A combination is a selection of items where the order does not matter. Ensure that each unique group of r elements is printed only once, regardless of order.
Example:
Input: arr = [1, 2, 3, 4], r = 2
Output: 1 2
1 3
1 4
2 3
2 4
3 4
Explanation: We need to generate all possible combinations of size 2 from an array of size 4. The total number of such combinations is given by the formula:
4C2 = 4! / [(4 - 2)! × 2!] = 6 combinations.Input: arr = [1, 2, 3, 4], r = 3
Output: 1 2 3
1 2 4
1 3 4
2 3 4
Explanation: We need to generate all possible combinations of size 3 from an array of size 4. The total number of such combinations is given by the formula:
4C3 = 4! / [(4 - 3)! × 3!] = 4 combinations.
Table of Content
The idea is to fix one element at a time at the current index and recursively fill the remaining positions. Starting from the first index, we try all possible elements that can be placed at that position such that the remaining elements can still fill the combination of size r. Once the size of the current combination becomes equal to r, we store or print that combination. To avoid duplicates, we only consider elements for the remaining positions that are on the right side of the current element.
Follow the below given step-by-step approach:
r.start index to the end of the array while ensuring there are enough remaining elements to fill the combination.r, add it to the result list.Following diagram shows recursion tree:
1 2 1 3 1 4 2 3 2 4 3 4
Time Complexity: O(r × C(n, r)), generates all combinations of size r from n elements, which is C(n, r) in total.
Each combination takes O(r) time to construct and store, leading to an overall complexity of O(r × C(n, r)).
Auxiliary Space: O(r), recursion depth reaches up to r, so the maximum stack space used is O(r). We are not including the space used for storing the final combinations
The idea is to explore each element in the array by making a binary choice—either include it in the current combination or skip it—while keeping track of the elements chosen so far. We recursively advance through the array, adding elements until the temporary combination reaches size r (at which point it’s recorded), then backtrack to explore all other possibilities. This guarantees that every valid combination of size r is generated.
Follow the below given step-by-step approach:
data to hold the current combination.(ind, r, data, result, arr).data.size() == r, add a copy of data to result and return.ind >= arr.size(), return (no more elements to process).arr[ind] to data.ind + 1 to include the current element.data (backtrack).ind + 1 to explore combinations that exclude the current element.Below is given the implementation:
1 2 1 3 1 4 2 3 2 4 3 4
The idea is to eliminate duplicate combinations by first sorting the input array so that identical elements are adjacent, and then, during the recursive generation, skipping any element that is the same as its immediate predecessor at the same recursion depth. This ensures that each unique value is only considered once per position in the combination, preventing repeated outputs.
Follow the below given step-by-step approach:
i from ind to n–1.i > ind and arr[i] == arr[i–1], skip this iteration to avoid duplicates.arr[i] in the current combination list and recurse with ind = i + 1.arr[i].r, add it to the result.Below is given the implementation:
1 1 1 2 1 3 1 4 2 3 2 4 3 4