2Brute2Best
🔥0

Count Subarrays with Zero Sum

Medium·GeeksforGeeks Pro OnlyBasic Reinforcement
GeeksforGeeks
01

Problem Statement

You are given an array of integers arr. The numbers can be positive, negative or zero.

Count how many contiguous, non-empty subarrays add up to exactly 0.

Write long long findSubarray(vector<int>& arr) that returns this count. Two subarrays are different if they start or end at a different index, even if they hold the same values.

Input

An integer array arr of length n. Values may be negative.

Output

The number of non-empty contiguous subarrays whose sum is 0 (as a 64-bit integer).

Example 1
Input: arr = [0, 0, 5, 5, 0, 0]
Output: 6

The zero-sum subarrays are [0] (index 0), [0] (index 1), [0, 0] (0..1), [0] (index 4), [0] (index 5) and [0, 0] (4..5).

Example 2
Input: arr = [6, -1, -3, 4, -2, 2, 4, 6, -12, -7]
Output: 4

They are [-1, -3, 4] (1..3), [-2, 2] (4..5), [-1, -3, 4, -2, 2] (1..5) and [-2, 2, 4, 6, -12] (4..8).

Example 3
Input: arr = [0]
Output: 1

The single element is a subarray with sum 0.

Constraints
  • • 1 ≤ n ≤ 10^6
  • • −10^9 ≤ arr[i] ≤ 10^9
  • • Prefix sums and the count can both exceed the int range — use 64-bit types.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“contiguous subarrays”
→ Each range sum is a difference of two prefix sums.
“sum exactly 0”
→ Look for earlier prefixes **equal** to the current one.
“count how many”
→ Store how many times each prefix appeared (a map of counts), not just whether it appeared (a set).
“negatives allowed”
→ A sliding window cannot be used; prefix sums do not care about signs.
03

Approach Complexity Ladder

Brute Force — try every startO(N²)O(1)

Fix each start, grow the end with a running sum, count every sum of 0

Optimal — prefix sum + count mapO(N)O(N)

count += how many earlier prefixes have the same total; seed seen[0] = 1

Brute Force Intuition

For every start, walk the end to the right while keeping a running sum of arr[start..end]. Each time the sum is 0, add one to the count.

Recognition rule for this sub-pattern:

Contiguous + exact sum, negatives allowed. Store how many times each prefix sum appeared; look up prefix − K.
Remember this · Prefix Sum
Trigger
Range sums, "subarray sum equals K" (especially with negatives), equal counts, divisible by K.
Think
If I knew the total up to every index, would the answer become a difference of two totals?
Key question
Which earlier prefix value am I looking for when I stand at index i?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.