#K42362. Zero Sum Subarray

    ID: 27071 Type: Default 1000ms 256MiB

Zero Sum Subarray

Zero Sum Subarray

Given an array of integers, determine whether there exists a non-empty contiguous subarray whose sum is zero. In other words, check if there exists indices i and j with 0 \leq i \leq j < n such that

$$ \sum_{k=i}^{j} a_k = 0 $$

If such a subarray exists, output YES; otherwise, output NO.

Example:

  • For the array [3, 4, -7, 1, 2], the subarray [3, 4, -7] sums to zero, so the output is YES.
  • For the array [1, 2, 3, 4], no subarray sums to zero hence the output is NO.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains a single integer n representing the number of elements in the array.
  2. The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single line to standard output (stdout):

  • YES if a non-empty contiguous subarray exists whose sum is zero.
  • NO otherwise.
## sample
5
3 4 -7 1 2
YES