Problem 354782 · easy · Level 03 Linear Management & Searching

Is There a Zero-Net Stretch?

prefix sums · hash set

Return True if some non-empty contiguous stretch of nums sums to exactly 0, and False otherwise.

Examples

Input:  nums = [4, -1, -3, 2]
Output: True
Explanation: 4 + (-1) + (-3) = 0.

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

Input:  nums = [0]
Output: True

Constraints

  • 0 <= len(nums) <= 10**5
  • -10**6 <= nums[i] <= 10**6
  • Target complexity: O(n) time. Checking every pair of endpoints is too slow.

Goals

  • Relate equal prefix totals to a zero-sum stretch
  • Use a set to detect a repeat in one pass
Starting Python…