#K59602. Subset Sum: Find All Valid Subsets
Subset Sum: Find All Valid Subsets
Subset Sum: Find All Valid Subsets
Given a list of integers and a target sum \(T\), your task is to find all subsets of the list whose elements sum exactly to \(T\). Each valid subset should be printed in a separate line with its elements separated by a single space. If there is no valid subset, print a single line containing "[]".
Formally, given an integer \(T\) and a list \(A = [a_1, a_2, \dots, a_N]\), find all subsets \(S \subseteq A\) such that:
[ \sum_{x \in S} x = T ]
You may output the subsets in any order.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains two integers \(N\) and \(T\), where \(N\) is the number of elements and \(T\) is the target sum.
- The second line contains \(N\) space-separated positive integers.
outputFormat
For each valid subset, print a line with the subset's elements separated by a single space. If no valid subset exists, output a single line containing "[]".
## sample4 5
1 2 3 4
1 4
2 3
</p>