#K59132. Combination Sum
Combination Sum
Combination Sum
You are given a list of integers and a target integer \( T \). Your task is to find all unique combinations of the numbers in the list that add up exactly to \( T \). Each number in the list may only be used once in each combination, and the solution set must not contain duplicate combinations. This is a typical backtracking problem. Note that a combination is considered unique if it does not exactly match any other combination when all numbers are sorted in non-decreasing order.
Mathematical Formulation:
Find all subsets \( S \) of the given set such that:
\[ \sum_{x \in S} x = T \]
inputFormat
The input is provided via standard input (stdin) and consists of two lines:
- The first line contains a series of space-separated integers representing the list.
- The second line contains a single integer \( T \) representing the target sum.
You may assume that the list contains at most 100 integers.
outputFormat
The output should be printed to standard output (stdout). If there is at least one valid combination, print each combination on a new line, with the numbers in the combination separated by a single space. The combinations should be printed in the order they are found by your algorithm. If no valid combination exists, output a single line containing []
.
10 1 2 7 6 1 5
8
1 1 6
1 2 5
1 7
2 6
</p>