#K36822. Combination Sum
Combination Sum
Combination Sum
Given an array of integers \(A\) and a target integer \(T\), find all unique combinations in \(A\) where the candidate numbers sum to \(T\). The same number may be chosen an unlimited number of times. Each combination should be listed in non‐decreasing order. Note that the solution set must not contain duplicate combinations.
For example, if \(A = [2,3,6,7]\) and \(T = 7\), one valid answer is \(\{[2,2,3],[7]\}\) because \(2+2+3=7\) and \(7=7\). If \(T = 0\), the only valid combination is an empty combination: \(\{[]\}\). If \(A\) is empty then no combination is possible (except when \(T=0\)).
inputFormat
The input is given via standard input. The first line contains two integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum. The second line contains \(n\) space-separated integers representing the array \(A\).
outputFormat
Print the list of all unique combinations in a Python list-like format. Each combination should be a list of integers in non-decreasing order. The entire output should be printed on a single line. If no combination exists, print an empty list []
. Note that when \(T=0\), output [[]]
to represent the single empty combination.
4 7
2 3 6 7
[[2, 2, 3], [7]]