File: find-subarrays-with-equal-sum/solution.py

Date: 2026-06-06

Time: 16:42

Purpose

This file solves LeetCode 2395: Find Subarrays With Equal Sum. It determines whether any two distinct length-2 subarrays in an integer array share the same sum. It owns exactly one responsibility: the equalsumsubarrays function, which is the solution entry point imported by the corresponding test file and (based on the imported_by list) referenced broadly across the test suite — likely via a shared test harness pattern.

Key Components

equalsumsubarrays(nums: list[int]) -> bool

Contract: Given a list of integers with length >= 2, returns True if there exist two length-2 subarrays starting at different indices whose element sums are equal.

The function uses a single-pass scan with a hash set for O(n) time and O(n) space.

Patterns

Sliding window + seen-set: Rather than computing all pairwise sums and comparing (O(n^2)), the code slides a width-2 window across the array, computing each sum on the fly. Each sum is checked against a seen set before being added — a standard duplicate-detection idiom. This is the minimal correct approach: no prefix-sum array, no sorting, just a set and one pass.

Early return: The function short-circuits on the first duplicate sum found, avoiding unnecessary work.

Dependencies

Imports: None — pure stdlib Python, no external dependencies.

Imported by: The function is imported by find-subarrays-with-equal-sum/testsolution.py directly. The massive importedby list in the prompt (300+ test files) is an artifact of the repo's test harness structure — those test files likely share a common import mechanism or test runner, not direct imports of this function.

Flow

1. Initialize an empty set seen.

2. Iterate i from 0 to len(nums) - 2 (inclusive).

3. Compute s = nums[i] + nums[i + 1] — the sum of the current length-2 subarray.

4. If s is already in seen, return True immediately.

5. Otherwise, add s to seen and continue.

6. If the loop completes without finding a duplicate, return False.

Invariants

Error Handling

None. The function trusts its caller to provide a valid list of integers. An empty list or single-element list silently returns False. Non-integer elements would propagate a TypeError from the + operator — no defensive handling.

Topics to Explore

Beliefs