Combinational sum problem with repetition of digits
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. You can use the numbers repeatedly to make the combination.
Input:
Test case T
T no. of N values and corresponding N positive numbers and the Number.
E.g.
3
3
2 3 5
12
3
2 7 5
14
4
1 2 3 4
5
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=3
Set[ ]=2 3 5
Number=12
Output:
2 2 2 2 2 2
2 2 2 3 3
2 2 3 5
2 5 5
3 3 3 3
Let there is a set S of positive numbers N and a positive number.
For pre-requisite, we are recommending you to go to our article combinational sum problem.
Making some combinations repeatedly using a number 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 backtracking approach.
Let, f(i) = function to insert the isth 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 from every (not f(2)) there will be two edges (f(3)) and (not f(3)) again from (not f(3)) there will be ( f(5)) and (not f(5)). 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