Problem

Longest Zero Sum Subarray

Learn this problem
Amazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

You are given an integer array nums containing positive and negative integers.

Return the length of the longest contiguous subarray whose sum is equal to 0. If no such subarray exists, return 0.

Function

longestZeroSumSubarray(nums: int[]) → int

Examples

Example 1

nums = [15, -2, 2, -8, 1, 7, 10, 23]return = 5

The subarray [-2, 2, -8, 1, 7] has sum 0 and length 5.

Example 2

nums = [1, 2, 3]return = 0

No non-empty contiguous subarray has sum 0.

Constraints

  • 1 <= nums.length
  • nums[i] may be positive, negative, or zero.

More Amazon problems

drafts saved locally
public int longestZeroSumSubarray(int[] nums) {
  // write your code here
}
nums[15, -2, 2, -8, 1, 7, 10, 23]
expected5
checking account