#C4746. Equal Subset Partition
Equal Subset Partition
Equal Subset Partition
You are given an integer n and an array of n integers. Your task is to determine whether the array can be partitioned into two subsets such that the sum of the elements in both subsets is equal.
In other words, if the total sum of the array is \(S\), you need to check if there exists a subset whose sum is \(\frac{S}{2}\). Note that if \(S\) is odd, then such a partition is impossible.
Example:
- For the input array
[1, 5, 11, 5]
, the output should beYES
because the array can be partitioned as[1, 5, 5]
and[11]
(both summing to 11). - For the input array
[1, 2, 3, 5]
, the output should beNO
because no equal-sum partition exists.
inputFormat
The first line contains an integer n representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single line containing YES
if the array can be partitioned into two subsets with equal sum; otherwise, output NO
.
4
1 5 11 5
YES
</p>