#K48227. Subset Sum: Find All Combinations

    ID: 28374 Type: Default 1000ms 256MiB

Subset Sum: Find All Combinations

Subset Sum: Find All Combinations

You are given an array of unique positive integers and a target integer \(T\). Your task is to find all unique subsets of the array that sum exactly to \(T\). Each subset must be sorted in non-decreasing order. In the output, first print the number of valid subsets found, followed by each subset on a new line with its elements separated by a single space. The overall list of subsets should be printed in lexicographical order (compare element by element).

If no subset sums to \(T\), simply output 0.

Input Format: The first line contains two integers, \(n\) (the number of elements in the array) and \(T\) (the target sum). The second line contains \(n\) space-separated integers.

Output Format: The first line contains a single integer \(m\) indicating the number of subsets found. Each of the next \(m\) lines contains one subset, with its numbers separated by a single space.

inputFormat

The input is given from standard input (stdin) and has the following format:

  • The first line contains two integers \(n\) and \(T\), where 1 \(\leq n \leq\) (a reasonable limit) and \(T\) is the target sum.
  • The second line contains \(n\) unique positive integers separated by spaces.

outputFormat

The output should be written to standard output (stdout) and formatted as follows:

  • The first line should contain a single integer \(m\), the number of subsets whose sum is exactly \(T\).
  • Then follow \(m\) lines, each line representing one subset. Each subset is a sorted list of integers (in non-decreasing order) separated by a space.

If no subset can be found, simply output 0.

## sample
4 16
2 4 6 10
2

2 4 10 6 10

</p>