Skip to content

Commit 376d1d9

Browse files
author
Krushit-Babariya
committed
Created FourSumProblem
1 parent e499d3b commit 376d1d9

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.misc;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class FourSumProblem {
8+
public List<List<Integer>> fourSum(int[] nums, int target) {
9+
int n = nums.length;
10+
List<List<Integer>> ans = new ArrayList<>();
11+
Arrays.sort(nums);
12+
13+
for (int i = 0; i < n; i++) {
14+
if (i > 0 && nums[i] == nums[i - 1]) {
15+
continue;
16+
}
17+
18+
for (int j = i + 1; j < n; j++) {
19+
if (j > i + 1 && nums[j] == nums[j - 1]) {
20+
continue;
21+
}
22+
23+
int k = j + 1;
24+
int l = n - 1;
25+
26+
while (k < l) {
27+
long sum = (long) nums[i] + nums[j] + nums[k] + nums[l];
28+
29+
if (sum == target) {
30+
ans.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
31+
k++;
32+
l--;
33+
34+
while (k < l && nums[k] == nums[k - 1]) {
35+
k++;
36+
}
37+
while (k < l && nums[l] == nums[l + 1]) {
38+
l--;
39+
}
40+
} else if (sum < target) {
41+
k++;
42+
} else {
43+
l--;
44+
}
45+
}
46+
}
47+
}
48+
return ans;
49+
}
50+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.misc;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class FourSumProblemTest {
10+
11+
private final FourSumProblem fourSumProblem = new FourSumProblem();
12+
13+
@Test
14+
public void testFourSum_WithValidInput() {
15+
int[] nums = {1, 0, -1, 0, -2, 2};
16+
int target = 0;
17+
List<List<Integer>> expected = Arrays.asList(
18+
Arrays.asList(-2, 0, 0, 2),
19+
Arrays.asList(-1, 0, 0, 1)
20+
);
21+
22+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
23+
assertEquals(expected.size(), result.size());
24+
assertTrue(result.containsAll(expected));
25+
}
26+
27+
@Test
28+
public void testFourSum_WithNoSolution() {
29+
int[] nums = {1, 2, 3, 4};
30+
int target = 100;
31+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
32+
assertTrue(result.isEmpty());
33+
}
34+
35+
@Test
36+
public void testFourSum_WithDuplicates() {
37+
int[] nums = {1, 1, 1, 1};
38+
int target = 4;
39+
List<List<Integer>> expected = Arrays.asList(
40+
Arrays.asList(1, 1, 1, 1)
41+
);
42+
43+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
44+
assertEquals(expected.size(), result.size());
45+
assertTrue(result.containsAll(expected));
46+
}
47+
48+
@Test
49+
public void testFourSum_WithNegativeNumbers() {
50+
int[] nums = {-3, -2, -1, 0, 0, 1, 2, 3};
51+
int target = 0;
52+
List<List<Integer>> expected = Arrays.asList(
53+
Arrays.asList(-3, 1, 1, 1),
54+
Arrays.asList(-2, 0, 0, 2),
55+
Arrays.asList(-1, 0, 0, 1)
56+
);
57+
58+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
59+
assertEquals(expected.size(), result.size());
60+
assertTrue(result.containsAll(expected));
61+
}
62+
63+
@Test
64+
public void testFourSum_WithEmptyArray() {
65+
int[] nums = {};
66+
int target = 0;
67+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
68+
assertTrue(result.isEmpty());
69+
}
70+
71+
@Test
72+
public void testFourSum_WithLessThanFourElements() {
73+
int[] nums = {1, 2, 3};
74+
int target = 6;
75+
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
76+
assertTrue(result.isEmpty());
77+
}
78+
}

0 commit comments

Comments
 (0)