O11

Subarrays Sum to Zero

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Given an array of integers, find the total number of contiguous non-empty subarrays whose elements sum to exactly 0.

Function Signature (Python)

zero_sum_subarrays(nums: list[int]) -> int

Input & Output Contract

Output Rule:Integer representing the count of zero-sum subarrays.

System Constraints

1 <= sayilar.length <= 10^5
-10^4 <= sayilar[i] <= 10^4

Example Execution Walkthrough

sayilar = [1, -1, 1, -1]
prefix sums: [1, 0, 1, 0]
zero-sum subarrays: [1, -1], [-1, 1], [1, -1], [1, -1, 1, -1] -> result 4

Example Test Scenarios (3)

Example 1
Input:[[1,-1,2,-2,3]]
Expected Output:3
Example 2: Üç Sıfır
Input:[[0,0,0]]
Expected Output:6
Example 3: Pozitif Dizi
Input:[[1,2,3]]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  [
    1,
    -1,
    2,
    -2,
    3
  ]
]
Expected Output
3