Skip to content

Commit 2416fd3

Browse files
committed
Update three sum algorithm
1 parent 430ca33 commit 2416fd3

File tree

8 files changed

+133
-118
lines changed

8 files changed

+133
-118
lines changed

src/java1/algorithms/array/ThreeSum.java

-67
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package threeSum;
2+
import java.util.*;
3+
4+
public class ThreeSum {
5+
// Two pointer approach:- TC: O(n logn) + O(n*2) => O(n * 2), SC: O(1)
6+
private static List<List<Integer>> threeSum(int[] nums, int target) {
7+
Arrays.sort(nums);
8+
List<List<Integer>> triplets = new ArrayList<>();
9+
10+
for(int i=0; i < nums.length-2; i++) {
11+
if(i > 0 && nums[i] == nums[i-1]) continue;
12+
13+
int left = i + 1, right = nums.length-1;
14+
15+
while(left < right) {
16+
int sum = nums[left] + nums[right] + nums[i] ;
17+
if(sum < target) {
18+
left++;
19+
} else if (sum > target) {
20+
right--;
21+
}
22+
else {
23+
24+
triplets.add(List.of(nums[i], nums[left], nums[right]));
25+
left++;
26+
while(left < right && nums[left] == nums[left-1]) left++;
27+
}
28+
}
29+
}
30+
return triplets;
31+
}
32+
33+
public static void main(String[] args) {
34+
int[] nums = {3, 3, -1, -2, 0, -2, 0, -1, 0, -1};
35+
System.out.println(threeSum(nums, 0));
36+
}
37+
}

src/java1/algorithms/array/threeSum/ThreeSum.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package threeSumDuplicates;
2+
import java.util.*;
3+
4+
// Hashing:- TC: O(n logn) + O(n*2) => O(n * 2), SC: O(n)
5+
public class ThreeSumDuplicates {
6+
private static List<List<Integer>> threeSum(int[] nums, int target) {
7+
List<List<Integer>> triplets = new ArrayList<>();
8+
HashSet<Integer> hashset = new HashSet<>();
9+
10+
for(int i=0; i< nums.length-2; i++) {
11+
for(int j=i+1; j< nums.length-1; j++) {
12+
int complement = target -(nums[i]+nums[j]);
13+
if(hashset.contains(complement) && !triplets.contains(List.of(nums[i], nums[j], complement))) {
14+
triplets.add(List.of(nums[i], nums[j], complement));
15+
} else {
16+
hashset.add(nums[j]);
17+
}
18+
}
19+
}
20+
return triplets;
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] nums = {3, 3, -1, -2, 0, -2, 0, -1, 0, -1};
25+
System.out.println(threeSum(nums, 0));
26+
}
27+
}

src/javascript/algorithms/array/threeSum.js

-51
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Two pointer approach:- TC: O(n logn) + O(n^2) => O(n^2), SC: O(1)
2+
function threeSum(nums, target) {
3+
nums.sort((a, b) => a-b);
4+
let triplets = [];
5+
6+
for(let i = 0; i < nums.length -2; i++) {
7+
//It avoids duplicates
8+
if(i > 0 && nums[i] === nums[i-1]) continue;
9+
10+
let left = i+1, right = nums.length -1;
11+
while(left < right) {
12+
let sum = nums[i] + nums[left] + nums[right];
13+
if(sum < target) {
14+
left++;
15+
} else if(sum > target) {
16+
right--;
17+
} else {
18+
triplets.push([nums[i], nums[left], nums[right]]);
19+
left++;
20+
//It avoids duplicates
21+
while(left < right && nums[left] === nums[left-1]) left++;
22+
}
23+
}
24+
}
25+
return triplets;
26+
}
27+
28+
let nums1 = [3, 3, -1, -2, 0, -2, 0, -1, 0, -1];
29+
let nums2 = [1, 3, 5, 1];
30+
console.log(threeSum(nums1, 0));
31+
console.log(threeSum(nums2, 0));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**Description:**
2+
Given an array of integers, return an array of triplets (in any order) such that `i != j != k` and `nums[i] + nums[j] + nums[k] = 0`.
3+
4+
Note that the solution set must not include duplicate triplets (i.e., [1, 0, 0] and [0, 1, 0] are duplicative).
5+
6+
## Examples:
7+
Example 1
8+
Input: [-1,0,1,2,-1,-4]
9+
Output: [[-1,-1,2],[-1,0,1]]
10+
11+
Example 2
12+
Input: [1,4,5,1]
13+
Output: []
14+
15+
Example 3
16+
Input: [0,0,0]
17+
Output: [[0,0,0]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Hashing:- TC: O(n logn) + O(n^2) => O(n^2), SC: O(n)
2+
function threeSumHashing(nums, target) {
3+
let triplets = [];
4+
let tripletSet = new Set();
5+
for(let i =0; i < nums.length - 2; i++) {
6+
for(let j=i+1; j< nums.length - 1; j++) {
7+
let complement = target - (nums[i] + nums[j]);
8+
if(tripletSet.has(complement)) {
9+
triplets.push([nums[i], nums[j], complement]);
10+
} else {
11+
tripletSet.add(nums[j]);
12+
}
13+
}
14+
}
15+
return triplets;
16+
}
17+
18+
let nums1 = [1, 2, 3, 4, 5, 6, 7, 8];
19+
let nums2 = [-1,0,1,2,-1,-4];
20+
console.log(threeSumHashing(nums1, 9));
21+
console.log(threeSumHashing(nums2, 0));

0 commit comments

Comments
 (0)