Given an array of positive integers arr[] and a sum x, find all unique combinations in arr[] where the sum is equal to x. The same repeated number may be chosen from arr[] unlimited number of times. Elements in a combination (a1, a2, âĶ, ak) must be printed in non-descending order. (ie, a1 <= a2 <= âĶ <= ak).
The combinations themselves must be sorted in ascending order, i.e., the combination with smallest first element should be printed first. If there is no combination possible the print "Empty" (without quotes).
Examples:
Input : arr[] = 2, 4, 6, 8
x = 8
Output : [2, 2, 2, 2]
[2, 2, 4]
[2, 6]
[4, 4]
[8]
Since the problem is to get all the possible results, not the best or the number of result, thus we don’t need to consider DP(dynamic programming), recursion is needed to handle it.
We should use the following algorithm.
1. Sort the array(non-decreasing).
2. First remove all the duplicates from array.
3. Then use recursion and backtracking to solve
the problem.
(A) If at any time sub-problem sum == 0 then
add that array to the result (vector of
vectors).
(B) Else if sum if negative then ignore that
sub-problem.
(C) Else insert the present array in that
index to the current vector and call
the function with sum = sum-ar[index] and
index = index, then pop that element from
current index (backtrack) and call the
function with sum = sum and index = index+1
Below is C++ implementation of above steps.
// C++ program to find all combinations that // sum to a given value #include <bits/stdc++.h> using namespace std; // Print all members of ar[] that have given void findNumbers(vector<int>& ar, int sum, vector<vector<int> >& res, vector<int>& r, int i) { // If current sum becomes negative if (sum < 0) return; // if we get exact answer if (sum == 0) { res.push_back(r); return; } // Recur for all remaining elements that // have value smaller than sum. while (i < ar.size() && sum - ar[i] >= 0) { // Till every element in the array starting // from i which can contribute to the sum r.push_back(ar[i]); // add them to list // recur for next numbers findNumbers(ar, sum - ar[i], res, r, i); i++; // remove number from list (backtracking) r.pop_back(); } } // Returns all combinations of ar[] that have given // sum. vector<vector<int> > combinationSum(vector<int>& ar, int sum) { // sort input array sort(ar.begin(), ar.end()); // remove duplicates ar.erase(unique(ar.begin(), ar.end()), ar.end()); vector<int> r; vector<vector<int> > res; findNumbers(ar, sum, res, r, 0); return res; } // Driver code int main() { vector<int> ar; ar.push_back(2); ar.push_back(4); ar.push_back(6); ar.push_back(8); int n = ar.size(); int sum = 8; // set value of sum vector<vector<int> > res = combinationSum(ar, sum); // If result is empty, then if (res.size() == 0) { cout << "Emptyn"; return 0; } // Print all combinations stored in res. for (int i = 0; i < res.size(); i++) { if (res[i].size() > 0) { cout << " ( "; for (int j = 0; j < res[i].size(); j++) cout << res[i][j] << " "; cout << ")"; } } } |
Output:
( 2 2 2 2 ) ( 2 2 4 ) ( 2 6 ) ( 4 4 ) ( 8 )
This article is contributed by Aditya Nihal Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of n digit numbers whose sum of digits equals to given sum
- Finding sum of digits of a number until sum becomes single digit
- Perfect Sum Problem (Print all subsets with given sum)
- Subset Sum Problem in O(sum) space
- Smallest number with given sum of digits and sum of square of digits
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Sum of sum-series of first N Natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Find an element in array such that sum of left array is equal to sum of right array
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Program for Sum of the digits of a given number
- Largest Sum Contiguous Subarray
- Maximum sum such that no two elements are adjacent
- Two elements whose sum is closest to zero
- Check for Children Sum Property in a Binary Tree
- Root to leaf path sum equal to a given number
- Subset Sum | Backtracking-4
- Convert a given tree to its Sum Tree
- Vertical Sum in a given Binary Tree | Set 1

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
