#2395
Easy Algorithms Find subarrays with equal sum
Array Hash Table
66.9% acceptance
Feb 25, 2026
612
33
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.
Solution
Rust
Time O(n)
Space O(n)
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
}
}