[LeetCode] Find Subarrays With Equal Sum

2395. Find Subarrays With Equal Sum

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool findSubarrays(vector<int>& nums) {
unordered_map<int,int> freq;
for(int i = 0; i < nums.size() - 1; i++) {
int sum = nums[i] + nums[i + 1];
if(freq.count(sum)) return true;
freq[sum]++;
}
return false;
}
};


Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/03/PS/LeetCode/find-subarrays-with-equal-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.