Skip to content

Commit 718cf03

Browse files
committed
FourSum Problem final
1 parent 65937b4 commit 718cf03

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@ public List<List<Integer>> fourSum1(int[] nums, int target) {
1818
if (nums == null || nums.length < 4) return ans;
1919
Arrays.sort(nums);
2020
for(int i = 0; i < nums.length-3; i++) {
21-
if(i > 0 && nums[i] == nums[i-1]) continue;
21+
if(i > 0 && nums[i] == nums[i-1]) {
22+
continue;
23+
}
2224
for(int j = i+1; j < nums.length - 2; j++) {
23-
if(j > i+1 && nums[j] == nums[j-1]) continue;
25+
if(j > i+1 && nums[j] == nums[j-1]) {
26+
continue;
27+
}
2428
int left = j+1;
2529
int right = nums.length -1;
2630
while(left < right) {
2731
long sum = (long)nums[i] + (long)nums[j] + (long)nums[left] + (long)nums[right];
2832
if(sum == target) {
2933
ans.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
30-
while (left < right && nums[left] == nums[left+1]) left++;
31-
while(left < right && nums[right] == nums[right-1]) right--;
34+
while (left < right && nums[left] == nums[left+1]) {
35+
left++;
36+
}
37+
while(left < right && nums[right] == nums[right-1]) {
38+
right--;
39+
}
3240
left++; right--;
3341
} else if (sum > target) {
3442
right--;
@@ -47,7 +55,9 @@ public List<List<Integer>> fourSum1(int[] nums, int target) {
4755
//Space Complexity - O(n^2) (Storing the pair of sums)
4856
public List<List<Integer>> fourSum2(int[] nums, int target) {
4957
List<List<Integer>> ans = new ArrayList<>();
50-
if (nums == null || nums.length < 4) return ans;
58+
if (nums == null || nums.length < 4) {
59+
return ans;
60+
}
5161
Arrays.sort(nums);
5262
for (int i = 0; i < nums.length - 3; i++) {
5363
if (i > 0 && nums[i] == nums[i-1]) continue;

src/test/java/com/thealgorithms/misc/FourSumProblemTest.java

+45
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,49 @@ public void testFourSum2_singleQuadruplet() {
9191
assertEquals(1, result4.size()); // Expect only one quadruplet
9292
assertTrue(result4.contains(List.of(2, 2, 2, 2)));
9393
}
94+
95+
@Test
96+
public void testFourSum1_withDuplicateFirstIndex() {
97+
FourSumProblem solver = new FourSumProblem();
98+
int[] nums = {1, 1, 0, 0, -1, -1, 2, 2};
99+
int target = 0;
100+
List<List<Integer>> result = solver.fourSum1(nums, target);
101+
assertTrue(!result.isEmpty()); // Expect some quadruplets
102+
}
103+
104+
@Test
105+
public void testFourSum1_withDuplicateSecondIndex() {
106+
FourSumProblem solver = new FourSumProblem();
107+
int[] nums = {2, 2, 2, 2, 2, 2, 0, 0};
108+
int target = 4;
109+
List<List<Integer>> result = solver.fourSum1(nums, target);
110+
assertEquals(1, result.size()); // Expect only one quadruplet
111+
assertTrue(result.contains(List.of(0, 0, 2, 2)));
112+
}
113+
114+
@Test
115+
public void testFourSum1_withSuccessfulSum() {
116+
FourSumProblem solver = new FourSumProblem();
117+
int[] nums = {1, 0, -1, 0, -2, 2};
118+
int target = 0;
119+
List<List<Integer>> result = solver.fourSum1(nums, target);
120+
assertEquals(3, result.size()); // Expect 3 quadruplets
121+
assertTrue(result.contains(List.of(-2, -1, 1, 2)));
122+
assertTrue(result.contains(List.of(-2, 0, 0, 2)));
123+
assertTrue(result.contains(List.of(-1, 0, 0, 1)));
124+
}
125+
126+
@Test
127+
public void testFourSum2_withDuplicateSecondIndex() {
128+
FourSumProblem solver = new FourSumProblem();
129+
130+
// Case with duplicates affecting the second loop index (j)
131+
int[] nums = {2, 2, 2, 2, 2, 0, 0};
132+
int target = 4;
133+
List<List<Integer>> result = solver.fourSum2(nums, target);
134+
135+
assertEquals(1, result.size()); // Expect only one quadruplet
136+
assertTrue(result.contains(List.of(0, 0, 2, 2))); // The only valid quadruplet
137+
}
138+
94139
}

0 commit comments

Comments
 (0)