Skip to content

Commit 65937b4

Browse files
committed
FourSum Problem
1 parent 17c3505 commit 65937b4

File tree

2 files changed

+96
-14
lines changed

2 files changed

+96
-14
lines changed

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

+2-14
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 static List<List<Integer>> fourSum1(int[] nums, int target) {
16+
public 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 static 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 static List<List<Integer>> fourSum2(int[] nums, int target) {
48+
public 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,16 +66,4 @@ public static 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-
}
8169
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.thealgorithms.misc;
2+
3+
import java.util.List;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class FourSumProblemTest {
10+
11+
@Test
12+
public void testFourSum1_basicCase() {
13+
FourSumProblem solver = new FourSumProblem();
14+
15+
// Case 1: Standard case with multiple quadruplets
16+
int[] nums1 = {1, 0, -1, 0, -2, 2};
17+
int target1 = 0;
18+
List<List<Integer>> result1 = solver.fourSum1(nums1, target1);
19+
assertEquals(3, result1.size()); // Expect 3 quadruplets
20+
assertTrue(result1.contains(List.of(-2, -1, 1, 2)));
21+
assertTrue(result1.contains(List.of(-2, 0, 0, 2)));
22+
assertTrue(result1.contains(List.of(-1, 0, 0, 1)));
23+
}
24+
25+
@Test
26+
public void testFourSum1_edgeCase_emptyArray() {
27+
FourSumProblem solver = new FourSumProblem();
28+
29+
// Case 2: Empty array
30+
int[] nums2 = {};
31+
int target2 = 0;
32+
List<List<Integer>> result2 = solver.fourSum1(nums2, target2);
33+
assertEquals(0, result2.size()); // Expect no quadruplets
34+
}
35+
36+
@Test
37+
public void testFourSum1_edgeCase_noQuadruplet() {
38+
FourSumProblem solver = new FourSumProblem();
39+
40+
// Case 3: No valid quadruplets
41+
int[] nums3 = {1, 2, 3, 4, 5};
42+
int target3 = 100;
43+
List<List<Integer>> result3 = solver.fourSum1(nums3, target3);
44+
assertEquals(0, result3.size()); // Expect no quadruplets
45+
}
46+
47+
@Test
48+
public void testFourSum2_basicCase() {
49+
FourSumProblem solver = new FourSumProblem();
50+
51+
// Case 4: Standard case with multiple quadruplets using HashMap approach
52+
int[] nums1 = {1, 0, -1, 0, -2, 2};
53+
int target1 = 0;
54+
List<List<Integer>> result1 = solver.fourSum2(nums1, target1);
55+
assertEquals(3, result1.size()); // Expect 3 quadruplets
56+
assertTrue(result1.contains(List.of(-2, -1, 1, 2)));
57+
assertTrue(result1.contains(List.of(-2, 0, 0, 2)));
58+
assertTrue(result1.contains(List.of(-1, 0, 0, 1)));
59+
}
60+
61+
@Test
62+
public void testFourSum2_edgeCase_emptyArray() {
63+
FourSumProblem solver = new FourSumProblem();
64+
65+
// Case 5: Empty array
66+
int[] nums2 = {};
67+
int target2 = 0;
68+
List<List<Integer>> result2 = solver.fourSum2(nums2, target2);
69+
assertEquals(0, result2.size()); // Expect no quadruplets
70+
}
71+
72+
@Test
73+
public void testFourSum2_edgeCase_noQuadruplet() {
74+
FourSumProblem solver = new FourSumProblem();
75+
76+
// Case 6: No valid quadruplets
77+
int[] nums3 = {1, 2, 3, 4, 5};
78+
int target3 = 100;
79+
List<List<Integer>> result3 = solver.fourSum2(nums3, target3);
80+
assertEquals(0, result3.size()); // Expect no quadruplets
81+
}
82+
83+
@Test
84+
public void testFourSum2_singleQuadruplet() {
85+
FourSumProblem solver = new FourSumProblem();
86+
87+
// Case 7: Single quadruplet in the array
88+
int[] nums4 = {2, 2, 2, 2, 2};
89+
int target4 = 8;
90+
List<List<Integer>> result4 = solver.fourSum2(nums4, target4);
91+
assertEquals(1, result4.size()); // Expect only one quadruplet
92+
assertTrue(result4.contains(List.of(2, 2, 2, 2)));
93+
}
94+
}

0 commit comments

Comments
 (0)