All problems
Zero-Sum Subarray Exists
Problem
Determine whether any contiguous subarray sums to zero.
Track a running prefix sum in a set initialized with 0. If the same prefix sum is seen twice, the elements between those two positions sum to zero.
Return True if such a subarray exists, otherwise False.
Hint
Seeding the set with 0 also catches subarrays that start at index 0.
Test cases
case 1
in: [[1,-1,2]]
expected: true
case 2
in: [[1,2,3]]
expected: false
case 3
in: [[]]
expected: false
case 4
in: [[0]]
expected: true
case 5
in: [[3,-3,1]]
expected: true
Starter codePython
def has_zero_sum_subarray(nums):
# Your code here
pass5 test cases · run your code against them in the editor