#K85672. Subset Sum Zero
Subset Sum Zero
Subset Sum Zero
You are given a list of integers. Your task is to determine whether there exists a non-empty subset of these integers whose sum is zero. In other words, you need to check if there is at least one selection of numbers (possibly one element) such that:
\(\sum_{i \in S} a_i = 0\)
where \(S\) is a non-empty subset of indices.
If such a subset exists, output YES
; otherwise, output NO
.
Note: The input is read from standard input (stdin) and the output is written to standard output (stdout).
inputFormat
The first line of input contains a single integer n
representing the number of elements in the array. The second line contains n
space-separated integers, representing the elements of the array.
For example:
5 1 2 -3 4 5
outputFormat
Output a single line containing YES
if there exists any non-empty subset of the array whose sum equals zero; otherwise, output NO
.
5
1 2 -3 4 5
YES