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
Find Subarrays With Equal Sum4240122412Subarray 1: [4,2] = 6Subarray 2: [2,4] = 6Equal sums found! → true
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
Asked in
Google 15 Amazon 12 Microsoft 8
24.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen