Problems > Zero-Sum Subarrays

Zero-Sum Subarrays

Difficulty: 1400 2000 ms 128 MB

prefix-sums

Count the contiguous subarrays whose elements sum to exactly zero.

Input: the first line has N; the second line has N integers.

Output: the number of zero-sum subarrays.

Constraints:

  • 1 ≤ N ≤ 105
  • −104 ≤ ai ≤ 104

Sample Tests

Input 1
6
3 -3 1 2 -3 4
Output 1
4
Input 2
3
0 0 0
Output 2
6

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.