From 782e1ee2d9199afe0d482f01511d8de98cf78618 Mon Sep 17 00:00:00 2001 From: ahtasham Date: Fri, 21 Apr 2023 12:49:29 +0500 Subject: [PATCH 1/4] feat: add new file --- src/_Problems_/3Sum/3sum.js | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/_Problems_/3Sum/3sum.js diff --git a/src/_Problems_/3Sum/3sum.js b/src/_Problems_/3Sum/3sum.js new file mode 100644 index 0000000..dcbef95 --- /dev/null +++ b/src/_Problems_/3Sum/3sum.js @@ -0,0 +1,39 @@ +var threeSum = function(nums) { + // sort the array + nums = nums.sort((a, b) => a - b); + + let result = []; + // iterate through the array and use two pointers to find the sum + for (let i = 0; i < nums.length; ++i) { + let left = i + 1; + let right = nums.length - 1; + while (left < right) { + let sum = nums[i] + nums[left] + nums[right]; + if (sum == 0) { + result.push([nums[i], nums[left], nums[right]]); + left++; + right--; + } + else if (sum < 0) { + left++; + } + else { + right--; + } + } + // skip duplicates + while (i < nums.length - 1 && nums[i] == nums[i + 1]) { + i++; + } + } + + // initialize set to remove duplicate + const set = new Set(result.map(JSON.stringify)); + // final output array + output = (new Array(...set).map(JSON.parse)); + return output; +}; + + +module.exports = threeSum; + From fc2218f7bcdaab90c6899044b8f087cf4ba10ada Mon Sep 17 00:00:00 2001 From: ahtasham Date: Fri, 21 Apr 2023 12:49:51 +0500 Subject: [PATCH 2/4] feat: test file of 3Sum algorithm --- src/_Problems_/3Sum/3sum.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/_Problems_/3Sum/3sum.test.js diff --git a/src/_Problems_/3Sum/3sum.test.js b/src/_Problems_/3Sum/3sum.test.js new file mode 100644 index 0000000..ad1114e --- /dev/null +++ b/src/_Problems_/3Sum/3sum.test.js @@ -0,0 +1,19 @@ +const threeSum = require("./3sum"); + +describe("threeSum", () => { + it("Should return [[-1, -1, 2], [-1, 0, 1]]", () => { + expect(threeSum([-1, 0, 1, 2, -1, -4])).toEqual([ + [-1, -1, 2], + [-1, 0, 1], + ]); + }); + + it("Should return [[0, 0, 0]]", () => { + expect(threeSum([0, 0, 0])).toEqual([[0, 0, 0]]); + }); + + it("Should return [[-1, -1, 2]]", () => { + expect(threeSum([-1, 2, -1, -4])).toEqual([[-1, -1, 2]]); + }); + +}); From 84b480e6c1d39c75dcc5553f6386bd7e4fe39258 Mon Sep 17 00:00:00 2001 From: ahtasham Date: Fri, 21 Apr 2023 12:50:37 +0500 Subject: [PATCH 3/4] feat: add 3sum problem --- TOC.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TOC.md b/TOC.md index 7765ff7..5824855 100644 --- a/TOC.md +++ b/TOC.md @@ -76,7 +76,7 @@ - [Next Greater for Every Element in an Array](src/_Problems_/next-greater-element) - [Compose Largest Number](src/_Problems_/compose-largest-number) - [Rotate Image](src/_Problems_/rotate-image) - +- [3 Sum](src/_Problems_/3Sum/) ### Searching - [Binary Search](src/_Searching_/BinarySearch) From 75a9df6a1e32d2abf0e87fb9b4aaab238eee26e4 Mon Sep 17 00:00:00 2001 From: ahtasham Date: Sun, 18 Jun 2023 20:00:51 +0500 Subject: [PATCH 4/4] refactor: made changes --- src/_Problems_/3Sum/3sum.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_Problems_/3Sum/3sum.js b/src/_Problems_/3Sum/3sum.js index dcbef95..6e7f536 100644 --- a/src/_Problems_/3Sum/3sum.js +++ b/src/_Problems_/3Sum/3sum.js @@ -1,4 +1,4 @@ -var threeSum = function(nums) { +const threeSum = function(nums) { // sort the array nums = nums.sort((a, b) => a - b);