Watch 10 video solutions for Find Subarrays With Equal Sum, a easy level problem involving Array, Hash Table. This walkthrough by Code with Alisha has 2,316 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices.
Return true if these subarrays exist, and false otherwise.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [4,2,4] Output: true Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.
Example 2:
Input: nums = [1,2,3,4,5] Output: false Explanation: No two subarrays of size 2 have the same sum.
Example 3:
Input: nums = [0,0,0] Output: true Explanation: The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
Constraints:
2 <= nums.length <= 1000-109 <= nums[i] <= 109Problem Overview: You are given an integer array and must determine whether two different subarrays of length 2 have the same sum. In other words, check if there exist indices i and j such that nums[i] + nums[i+1] == nums[j] + nums[j+1] with i != j. The task reduces to computing sums of adjacent pairs and detecting duplicates.
Approach 1: Brute Force Comparison (Time: O(n²), Space: O(1))
The straightforward method compares every pair of length-2 subarrays. First iterate through the array and compute the sum for each adjacent pair. For every pair starting at index i, run another loop for indices j > i and check whether nums[i] + nums[i+1] equals nums[j] + nums[j+1]. If a match appears, return true. This approach works because it exhaustively checks all possible pair combinations, but the nested loops make the runtime quadratic. With arrays up to large sizes, O(n²) quickly becomes inefficient. It uses constant extra memory since only a few variables are stored during comparisons.
Approach 2: HashSet for Pair Sums (Time: O(n), Space: O(n))
A more efficient solution stores each adjacent-pair sum in a HashSet. Iterate once through the array and compute pairSum = nums[i] + nums[i+1]. Before inserting the value, check if the set already contains that sum. If it does, two subarrays share the same total and the answer is immediately true. Otherwise, insert the sum and continue scanning. This works because a hash set provides average O(1) lookup and insertion, turning duplicate detection into a linear-time process.
The key insight is that the problem does not require storing indices or the actual subarrays. Only the pair sums matter. As soon as a duplicate sum appears, two different adjacent pairs must produce it. This pattern—tracking seen values to detect duplicates—is common in hash table problems and appears frequently in array scanning tasks.
Recommended for interviews: The HashSet approach is what interviewers expect. The brute force method demonstrates you understand the problem constraints and baseline logic, but the optimized solution shows you recognize duplicate detection patterns and can apply a hash-based lookup to reduce the time complexity from O(n²) to O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Comparison | O(n²) | O(1) | Good for understanding the problem or when the array size is very small |
| HashSet Duplicate Pair Sum | O(n) | O(n) | Best general solution for detecting repeated adjacent pair sums efficiently |