@@ -13,7 +13,7 @@ public class FourSumProblem {
13
13
//Best approach - Sorting and two-pointers
14
14
//Time Complexity - O(n^3)
15
15
//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 ) {
17
17
List <List <Integer >> ans = new ArrayList <>();
18
18
if (nums == null || nums .length < 4 ) return ans ;
19
19
Arrays .sort (nums );
@@ -45,7 +45,7 @@ public List<List<Integer>> fourSum1(int[] nums, int target) {
45
45
//Another approach - Using HashMap
46
46
//Time Complexity - O(n^3)
47
47
//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 ) {
49
49
List <List <Integer >> ans = new ArrayList <>();
50
50
if (nums == null || nums .length < 4 ) return ans ;
51
51
Arrays .sort (nums );
@@ -66,4 +66,16 @@ public List<List<Integer>> fourSum2(int[] nums, int target) {
66
66
}
67
67
return ans ;
68
68
}
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
+ }
69
81
}
0 commit comments