#C4626. Combination Sum for Backtracking Challenge
Combination Sum for Backtracking Challenge
Combination Sum for Backtracking Challenge
You are given an array of positive integers nums and a target sum T. Your task is to find all unique combinations in nums where the chosen numbers sum to T. You can use the same number as many times as needed. Each valid combination should be printed on a separate line with its numbers separated by a single space. If no valid combination exists for a test case, output No combination
.
Note that the solution is expected to use a backtracking approach to explore all potential combinations. The output for different test cases should be separated by an empty line.
inputFormat
The input is read from standard input (stdin) and follows the format below:
T n₁ T₁ nums₁[0] nums₁[1] ... nums₁[n₁-1] n₂ T₂ nums₂[0] nums₂[1] ... nums₂[n₂-1] ...
Here, T
is the number of test cases. For each test case, the first line contains two integers: n
(the number of elements in nums) and T
(the target sum). The second line contains n
space-separated positive integers.
outputFormat
For each test case, output all unique combinations whose sum equals the target. Each combination should be printed on a separate line with the numbers separated by a space. If there are multiple combinations for a test case, separate them with a newline. If no valid combination exists, print No combination
. The outputs for different test cases must be separated by an empty line.
1
4 7
2 3 6 7
2 2 3
7
</p>