Find Subarrays With Equal Sum
EasyUpdated: Aug 2, 2025
Practice on:
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, andfalse otherwise.
A subarray is a contiguous non-empty sequence of elements within an array.
Examples
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-10^9 <= nums[i] <= 10^9
Solution
Method 1 – Hash Set for Subarray Sums
Intuition
For each subarray of length 2, we can compute its sum and check if we've seen this sum before. If so, there are two subarrays with equal sum.
Approach
- Initialize an empty hash set to store sums of subarrays of length 2.
- Iterate through the array, for each index
ifrom 0 ton-2:- Compute the sum of
nums[i] + nums[i+1]. - If this sum is already in the set, return
true. - Otherwise, add the sum to the set.
- Compute the sum of
- If no duplicate sum is found, return
false.
Code
C++
class Solution {
public:
bool findSubarrays(vector<int>& nums) {
unordered_set<int> seen;
for (int i = 0; i + 1 < nums.size(); ++i) {
int s = nums[i] + nums[i+1];
if (seen.count(s)) return true;
seen.insert(s);
}
return false;
}
};
Go
func findSubarrays(nums []int) bool {
seen := map[int]struct{}{}
for i := 0; i+1 < len(nums); i++ {
s := nums[i] + nums[i+1]
if _, ok := seen[s]; ok {
return true
}
seen[s] = struct{}{}
}
return false
}
Java
class Solution {
public boolean findSubarrays(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int i = 0; i + 1 < nums.length; i++) {
int s = nums[i] + nums[i+1];
if (seen.contains(s)) return true;
seen.add(s);
}
return false;
}
}
Kotlin
class Solution {
fun findSubarrays(nums: IntArray): Boolean {
val seen = mutableSetOf<Int>()
for (i in 0 until nums.size-1) {
val s = nums[i] + nums[i+1]
if (s in seen) return true
seen.add(s)
}
return false
}
}
Python
class Solution:
def findSubarrays(self, nums: list[int]) -> bool:
seen = set()
for i in range(len(nums)-1):
s = nums[i] + nums[i+1]
if s in seen:
return True
seen.add(s)
return False
Rust
impl Solution {
pub fn find_subarrays(nums: Vec<i32>) -> bool {
use std::collections::HashSet;
let mut seen = HashSet::new();
for i in 0..nums.len()-1 {
let s = nums[i] + nums[i+1];
if !seen.insert(s) {
return true;
}
}
false
}
}
TypeScript
class Solution {
findSubarrays(nums: number[]): boolean {
const seen = new Set<number>();
for (let i = 0; i + 1 < nums.length; i++) {
const s = nums[i] + nums[i+1];
if (seen.has(s)) return true;
seen.add(s);
}
return false;
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length ofnums, since we scan the array once. - 🧺 Space complexity:
O(n), for the hash set of subarray sums.