Skip to content

Commit 2f4e3f0

Browse files
committed
Added four sum problem
1 parent bcf4034 commit 2f4e3f0

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.misc;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import org.apache.commons.lang3.tuple.Pair;
7+
8+
public class FourSumProblem {
9+
10+
public static List<Pair<Integer, Integer>> fourSum(int[] values, int target) {
11+
List<Pair<Integer, Integer>> result = new ArrayList<>();
12+
if (values == null || values.length < 4) {
13+
return result;
14+
}
15+
16+
Arrays.sort(values);
17+
18+
for (int i = 0; i < values.length - 3; i++) {
19+
if (i > 0 && values[i] == values[i - 1]) {
20+
continue; // Skip duplicates
21+
}
22+
for (int j = i + 1; j < values.length - 2; j++) {
23+
if (j > i + 1 && values[j] == values[j - 1]) {
24+
continue; // Skip duplicates
25+
}
26+
27+
int left = j + 1;
28+
int right = values.length - 1;
29+
30+
while (left < right) {
31+
int sum = values[i] + values[j] + values[left] + values[right];
32+
if (sum == target) {
33+
result.add(Pair.of(left, right));
34+
35+
// Skip duplicates
36+
while (left < right && values[left] == values[left + 1]) left++;
37+
while (left < right && values[right] == values[right - 1]) right--;
38+
39+
left++;
40+
right--;
41+
} else if (sum < target) {
42+
left++;
43+
} else {
44+
right--;
45+
}
46+
}
47+
}
48+
}
49+
return result;
50+
}
51+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
6+
import org.apache.commons.lang3.tuple.Pair;
7+
import org.junit.jupiter.api.Test;
8+
import java.util.List;
9+
10+
/**
11+
* Test case for Four sum Problem.
12+
*/
13+
public class FourSumProblemTest {
14+
15+
@Test
16+
void testFourSumExists() {
17+
final int[] values = new int[] {1, 0, -1, 0, -2, 2};
18+
final int target = 0;
19+
// Expecting one solution (e.g., indices of the numbers summing to target)
20+
final var expected = List.of(
21+
Pair.of(0, 5), // -2 + 2 + 1 + -1 = 0
22+
Pair.of(1, 4) // 0 + 0 + -1 + 1 = 0
23+
);
24+
assertEquals(expected, FourSumProblem.fourSum(values, target));
25+
}
26+
27+
@Test
28+
void testFourSumNoSolution() {
29+
final int[] values = new int[] {1, 2, 3, 4};
30+
final int target = 100;
31+
assertFalse(FourSumProblem.fourSum(values, target).isEmpty());
32+
}
33+
34+
@Test
35+
void testFourSumMultipleSolutions() {
36+
final int[] values = new int[] {1, 0, -1, 0, -2, 2};
37+
final int target = 0;
38+
final var expected = List.of(
39+
Pair.of(0, 5), // -2 + 2 + 1 + -1 = 0
40+
Pair.of(1, 4) // 0 + 0 + -1 + 1 = 0
41+
);
42+
assertEquals(expected, FourSumProblem.fourSum(values, target));
43+
}
44+
45+
@Test
46+
void testFourSumNegativeNumbers() {
47+
final int[] values = new int[] {-1, -2, -3, -4, -5, 5};
48+
final int target = -10;
49+
final var expected = List.of(
50+
Pair.of(1, 3), // -2 + -3 + -5 + 5 = -10
51+
Pair.of(0, 4) // -1 + -4 + -5 + 5 = -10
52+
);
53+
assertEquals(expected, FourSumProblem.fourSum(values, target));
54+
}
55+
56+
@Test
57+
void testFourSumNoSolutionDuplicatedInputs() {
58+
final int[] values = new int[] {1, 1, 1, 1};
59+
final int target = 10;
60+
assertFalse(FourSumProblem.fourSum(values, target).isEmpty());
61+
}
62+
}

0 commit comments

Comments
 (0)