#C3677. Subarray with Zero Sum

    ID: 47130 Type: Default 1000ms 256MiB

Subarray with Zero Sum

Subarray with Zero Sum

You are given an array of integers. Your task is to determine whether there is a contiguous subarray (of size at least 1) whose sum is equal to zero.

For each test case, the first line contains an integer n which represents the number of elements in the array. The next line contains n space-separated integers: \(a_1, a_2, \ldots, a_n\). Your program should output "YES" if there exists at least one contiguous subarray with a sum of zero, otherwise output "NO". Each result should be printed on a new line.

Hint: You may use the concept of prefix sums. If at any index the prefix sum becomes zero or the same prefix sum appears more than once, then there exists a subarray with sum zero.

inputFormat

The input is read from stdin and has the following format:

T
n
a1 a2 ... an
... (repeated T times)

Where:

  • T is the number of test cases.
  • For each test case, the first line contains an integer n (\(1 \le n \le 10^5\)).
  • The next line contains n space-separated integers \(a_1, a_2, \ldots, a_n\) (\(|a_i| \le 10^9\)).

outputFormat

For each test case, output a single line from stdout containing "YES" if there exists a contiguous subarray with zero sum, otherwise output "NO".

## sample
2
5
4 2 -3 1 6
5
4 2 0 1 6
YES

YES

</p>