Combinational sum problem
Given a set of positive numbers and a number, your task is to find out the combinations of the numbers from the set whose summation equals to the given number.
Input:
Test case T
T no. of N values and corresponding N positive numbers and the Number.
E.g.
3
6
4 1 3 2 4 5
8
7
4 5 2 5 1 3 4
10
7
10 1 2 7 6 1 5
8
Constrains:
1 <= T <= 500
1 <= N <= 20
1 <= A[i] <= 9
1 <= Number<= 50
Output:
Print all the combination which summation equals to the given number.
Example
Input:
N = 7
Set[] = 10 1 2 7 6 1 5
Number = 8
Output:
1 1 6
1 2 5
1 7
2 6
Let there is a set S of positive numbers N and a positive number.
Making some combinations in such a way that the summation of that combination results that given number is a problem of combination and we will solve this problem using a backtracking approach.
Let, f(i) = function to insert the ith number into the combinational subset.
In this case, we will consider two cases to solve the problem,
And every time we will check the current sum with the number. Each of the time we will count the number of occurrence and the also the combinations.
Let, f(i) = function to insert the ith number into the combinational subset.
For the input:
Here in this case we will discard that edges which have a current sum greater than the given number and make a count to those numbers which are equal to the given number.
C++ implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer