#C3146. Equal Subset Partition

    ID: 46541 Type: Default 1000ms 256MiB

Equal Subset Partition

Equal Subset Partition

Given an array of positive integers, determine whether it is possible to partition the array into two subsets such that the sum of the elements in both subsets is equal. If such a partition exists, output the two subsets; otherwise, output an empty list.

The mathematical formulation is as follows. Let \(S\) be the sum of all elements in the array. If \(S\) is odd then the partition is impossible. Otherwise, find a subset whose sum equals \(\frac{S}{2}\). More formally, given an array \(a_1, a_2, \dots, a_n\), find a subset \(I \subseteq \{1,2,\dots,n\}\) such that

[ \sum_{i \in I} a_i = \frac{1}{2}\sum_{i=1}^n a_i. ]

If such an \(I\) exists, output the subset corresponding to \(I\) and its complement; otherwise output [].

inputFormat

The first line contains an integer \(n\) indicating the number of elements in the array.

The second line contains \(n\) space-separated integers representing the array.

outputFormat

If a valid partition exists, output the two subsets in one line separated by a space. Each subset should be printed as a list (e.g., [1, 2, 3]).

If no valid partition exists, output [].

## sample
4
1 5 11 5
[5, 5, 1] [11]

</p>