Find Subarrays With Equal Sum - Problem
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.
Input & Output
Example 1 — Found Equal Sums
$
Input:
nums = [4,2,4]
›
Output:
true
💡 Note:
We can take subarrays [4,2] (sum=6) and [2,4] (sum=6). Both have the same sum of 6.
Example 2 — No Equal Sums
$
Input:
nums = [1,2,3,4]
›
Output:
false
💡 Note:
Subarrays are [1,2] (sum=3), [2,3] (sum=5), [3,4] (sum=7). All sums are different.
Example 3 — Too Short Array
$
Input:
nums = [0,0]
›
Output:
false
💡 Note:
Only one subarray [0,0] possible, need at least two different subarrays.
Constraints
- 2 ≤ nums.length ≤ 1000
- -1000 ≤ nums[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array with adjacent pairs to check
2
Calculate Sums
Find sum of each length-2 subarray
3
Check for Duplicates
Return true if any two sums match
Key Takeaway
🎯 Key Insight: Use a hash set to detect duplicate sums in O(n) time instead of comparing all pairs
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code