内容中心

2026年热门的皮带输送机滚筒优质公司推荐-沧州路博机械制造有限公司

To solve this problem, we need to find the length of the longest subarray with a sum of zero. The optimal approach involves using a hash map (dictionary) to track the first occurrence of each prefix sum, which allows us to efficiently determine the longest subarray with a sum of zero.

Approach

  1. Prefix Sum Concept: The prefix sum up to an index i is the sum of all elements from the start of the array to index i. If the prefix sum at index j (where j > i) is the same as the prefix sum at index i, the subarray from i+1 to j has a sum of zero.
  2. Hash Map Usage: We use a hash map to store the first occurrence of each prefix sum. This helps in calculating the length of the subarray with a sum of zero by subtracting the first occurrence index from the current index.
  3. Initialization: We start with a prefix sum of 0 at index -1 to handle cases where the subarray starts from the beginning of the array (sum of elements from index 0 to i is zero).

Solution Code

def longest_zero_sum_subarray(arr):
    prefix_sum = 0
    max_length = 0
    sum_index_map = {0: -1}  # Maps prefix sum to its first occurrence index

    for i in range(len(arr)):
        prefix_sum += arr[i]

        if prefix_sum in sum_index_map:
            # Calculate the length of the subarray with sum zero
            current_length = i - sum_index_map[prefix_sum]
            if current_length > max_length:
                max_length = current_length
        else:
            # Store the first occurrence of this prefix sum
            sum_index_map[prefix_sum] = i

    return max_length

Explanation

  1. Initialization: We initialize prefix_sum to 0 and max_length to 0 (since the minimum length of a subarray with sum zero is 0 if no such subarray exists). The sum_index_map starts with {0: -1} to handle subarrays starting from index 0.
  2. Iterate Through Array: For each element in the array, we update the prefix_sum by adding the current element.
  3. Check Prefix Sum: If the prefix_sum is already in the map, we calculate the length of the subarray from the first occurrence index to the current index. If this length is greater than max_length, we update max_length.
  4. Store New Prefix Sum: If the prefix_sum is not in the map, we store it with the current index (since we want the first occurrence).

Key Points

  • Time Complexity: O(n) (we traverse the array once, and each hash map operation is O(1)).
  • Space Complexity: O(n) (in the worst case, all prefix sums are unique, so the hash map stores n+1 entries).

This approach efficiently finds the longest subarray with a sum of zero using prefix sums and a hash map, making it optimal for large input sizes.

Example Test Cases:

  • Input: [1, 2, -3, 3] → Output: 3 (subarray [1,2,-3]).
  • Input: [0,0,0] → Output: 3 (entire array).
  • Input: [1,-1,2,-2] → Output: 4 (entire array).
  • Input: [5] → Output: 0 (no subarray with sum zero).
  • Input: [0] → Output: 1 (single element zero).

All these test cases are handled correctly by the solution.

沧州路博机械制造有限公司

沧州路博机械制造有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?