forked from ignacio-chiazzo/Algorithms-Leetcode-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Subarrays_With_Equal_Sum.js
51 lines (35 loc) · 1.29 KB
/
Find_Subarrays_With_Equal_Sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Find Subarrays With Equal Sum
https://leetcode.com/problems/find-subarrays-with-equal-sum/description/
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.
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.
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
var findSubarrays = function (nums) {
const sumsSeen = new Set();
for (let i = 0; i < nums.length - 1; i++) {
if (sumsSeen.has(nums[i] + nums[i + 1])) {
return true;
}
sumsSeen.add(nums[i] + nums[i + 1]);
}
return false;
};
module.exports.findSubarrays = findSubarrays;