Skip to content

Commit 17c3505

Browse files
committed
FourSum Problem resolved
1 parent 7a42870 commit 17c3505

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/main/java/com/thealgorithms/misc/FourSumProblem.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FourSumProblem {
1313
//Best approach - Sorting and two-pointers
1414
//Time Complexity - O(n^3)
1515
//Space Complexity - O(n)
16-
public List<List<Integer>> fourSum1(int[] nums, int target) {
16+
public static List<List<Integer>> fourSum1(int[] nums, int target) {
1717
List<List<Integer>> ans = new ArrayList<>();
1818
if (nums == null || nums.length < 4) return ans;
1919
Arrays.sort(nums);
@@ -45,7 +45,7 @@ public List<List<Integer>> fourSum1(int[] nums, int target) {
4545
//Another approach - Using HashMap
4646
//Time Complexity - O(n^3)
4747
//Space Complexity - O(n^2) (Storing the pair of sums)
48-
public List<List<Integer>> fourSum2(int[] nums, int target) {
48+
public static List<List<Integer>> fourSum2(int[] nums, int target) {
4949
List<List<Integer>> ans = new ArrayList<>();
5050
if (nums == null || nums.length < 4) return ans;
5151
Arrays.sort(nums);
@@ -66,4 +66,16 @@ public List<List<Integer>> fourSum2(int[] nums, int target) {
6666
}
6767
return ans;
6868
}
69+
70+
public static void main(String[] args) {
71+
int[] arr1 = {1, 0, -1, 0, -2, 2};
72+
int target1 = 0;
73+
System.out.println(fourSum1(arr1, target1));
74+
System.out.println(fourSum2(arr1, target1));
75+
76+
int[] arr2 = {4, 3, 3, 4, 4, 2, 1, 2, 1, 1};
77+
int target2 = 9;
78+
System.out.println(fourSum2(arr2, target2));
79+
System.out.println(fourSum2(arr2, target2));
80+
}
6981
}

0 commit comments

Comments
 (0)