#K40987. Equal Sum Subarrays
Equal Sum Subarrays
Equal Sum Subarrays
You are given an array of integers. Your task is to determine whether there exist two non-overlapping contiguous subarrays (i.e. subarrays that do not share any indices) such that the sum of the elements in the first subarray is equal to the sum of the elements in the second subarray.
Formally, given an array \(a\) of length \(n\), determine if there exist indices \(i, j, k, l\) satisfying
[ 0 \leq i \leq j < k \leq l < n, ]
such that
[ \sum_{p=i}^{j} a_p = \sum_{q=k}^{l} a_q. ]
If such subarrays exist, print YES
. Otherwise, print NO
.
inputFormat
The first line contains an integer \(T\) representing the number of test cases. The description of the test cases follows.
For each test case:
- The first line contains an integer \(n\) indicating the size of the array.
- The second line contains \(n\) space-separated integers representing the elements of the array.
Input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing YES
if there exist two non-overlapping contiguous subarrays with equal sum, or NO
otherwise.
Output is written to standard output (stdout).
## sample6
4
2 4 -2 4
3
1 3 2
1
5
4
1 1 1 1
3
1000000 -1000000 0
5
-1000000 1000000 -1000000 1000000 0
YES
NO
NO
YES
YES
YES
</p>