Longest Subarray with Sum 0

Quick Overview

Find the length of the longest contiguous subarray of an integer array whose elements sum to zero, returning 0 when there is none. It tests prefix sum reasoning with a hash map, the handling of negative values, and getting from a quadratic scan of all pairs down to linear time.

Longest Subarray with Sum 0

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums`, return the length of the longest contiguous subarray whose elements sum to `0`. If no such subarray exists, return `0`. ### Function Signature ```python def longest_zero_sum_subarray(nums: list[int]) -> int: ``` ### Rules - A subarray is a non-empty contiguous block `nums[i..j]` with `i <= j`. - Elements may be negative, zero or positive. A single element equal to `0` is a subarray of length 1. - Only the length is returned, so it does not matter which of several longest subarrays is meant. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [15,-2,2,-8,1,7,10,23] Output: 5 ``` The subarray `[-2,2,-8,1,7]` sums to 0 and has length 5. Every subarray of length 6 or more has a nonzero sum: the three of length 6 sum to 15, 10 and 35, the two of length 7 sum to 25 and 33, and the whole array sums to 48. ```text Input: nums = [1,2,3] Output: 0 ``` All elements are positive, so every subarray has a positive sum. ```text Input: nums = [1,-1,3,2,-2,-3,3] Output: 6 ``` The first six elements sum to `1 - 1 + 3 + 2 - 2 - 3 = 0`. The whole array sums to 3, and the other subarray of length 6, `[-1,3,2,-2,-3,3]`, sums to 2.

Overview: Find the length of the longest contiguous subarray of an integer array whose elements sum to zero, returning 0 when there is none. It tests prefix sum reasoning with a hash map, the handling of negative values, and getting from a quadratic scan of all pairs down to linear time.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return the length of the longest contiguous subarray whose elements sum to 0.

If no such subarray exists, return 0.

Function Signature

def longest_zero_sum_subarray(nums: list[int]) -> int:

Rules

  • A subarray is a non-empty contiguous block nums[i..j] with i <= j .
  • Elements may be negative, zero or positive. A single element equal to 0 is a subarray of length 1.
  • Only the length is returned, so it does not matter which of several longest subarrays is meant.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [15,-2,2,-8,1,7,10,23]
Output: 5

The subarray [-2,2,-8,1,7] sums to 0 and has length 5. Every subarray of length 6 or more has a nonzero sum: the three of length 6 sum to 15, 10 and 35, the two of length 7 sum to 25 and 33, and the whole array sums to 48.

Input: nums = [1,2,3]
Output: 0

All elements are positive, so every subarray has a positive sum.

Input: nums = [1,-1,3,2,-2,-3,3]
Output: 6

The first six elements sum to 1 - 1 + 3 + 2 - 2 - 3 = 0. The whole array sums to 3, and the other subarray of length 6, [-1,3,2,-2,-3,3], sums to 2.