-
Notifications
You must be signed in to change notification settings - Fork 97
Adding Find Subarrays With Equal Sum #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
10 changes: 10 additions & 0 deletions
10
LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const assert = require('assert'); | ||
const findSubarrays = require('../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum').findSubarrays; | ||
|
||
var test = function () { | ||
assert.equal(findSubarrays([4,2,4]), true); | ||
assert.equal(findSubarrays([1,2,3,4,5]), false); | ||
assert.equal(findSubarrays([0,0,0]), true); | ||
} | ||
|
||
module.exports.test = test; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you ident this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I tried to follow the same format as the others in the readme. Indent what, could you explain please? The preview of the readme looks fine so I'm maybe missing something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be github but I see the | medium | not aligned. Anyways, this is good to merge, thank you!