#K43592. Subset with Half Sum
Subset with Half Sum
Subset with Half Sum
You are given an array of positive integers. Your task is to determine whether there exists a subset of these numbers whose sum is equal to half of the total sum of the array.
Let \(S\) be the total sum of the array. The goal is to find a subset \(A\) such that
\(\sum_{x \in A} x = \frac{S}{2}\)
If such a subset exists, output its elements in non-decreasing order separated by a single space. Otherwise, output []
.
It is guaranteed that the input consists of a valid array of positive integers. Use an efficient algorithm since the array may contain up to several hundred elements.
inputFormat
The input is given via standard input (stdin) and has the following format:
n x1 x2 x3 ... xn
Here, n
is the number of elements in the array, and x1, x2, ..., xn
are the positive integers in the array.
outputFormat
If a subset exists whose sum equals half of the total sum, output the subset's elements in non-decreasing order separated by a space. If no such subset exists, output []
.
4
1 5 11 5
1 5 5