Skip to main content

Practice · 4 of 5

Zero-Sum Subarrays

CHALLENGE
Difficulty: intermediate+25 XP

**Description:** Count the subarrays a[l..r] whose elements sum to exactly 0. The answer can reach n*(n+1)/2 — use 64-bit integers. **Input:** Line 1: n (1 <= n <= 2*10^5). Line 2: n integers (-10^9 <= a_i <= 10^9). **Output:** One integer — the number of zero-sum subarrays. **Example:** 5 / 1 -1 2 -2 3 -> 3 (the subarrays 1..2, 3..4, 1..4).

Back to lesson: Practice: Prefix Sum Problem Set