#P1537. Equal Marble Partition
Equal Marble Partition
Equal Marble Partition
Martha and Bill each have their own collection of marbles. They want to redistribute the marbles so that both have equal total value. Each marble is assigned an integer value between $1$ and $6$. They want to check whether it is possible to divide all the marbles into two groups such that the sum of the values in each group is equal.
If the marbles' values can be partitioned into two subsets with equal sum, output YES
, otherwise output NO
.
For example, if there is one marble of value $1$, one of value $3$, and two of value $4$, then it is impossible to split them into two groups with equal total values.
This problem can be modeled as a subset sum problem where we need to check if there exists a subset whose sum is equal to half of the total sum of all marbles. Note that a necessary (but not sufficient) condition is that the total sum is even.
inputFormat
The first line contains an integer N
representing the number of marbles.
The second line contains N
space-separated integers, each representing the value of a marble (an integer between 1
and 6
).
outputFormat
Output YES
if it is possible to partition the marbles into two groups with equal total value. Otherwise, output NO
.
sample
4
1 3 4 4
NO