diff --git a/src/main/java/com/thealgorithms/misc/FourSumProblem.java b/src/main/java/com/thealgorithms/misc/FourSumProblem.java new file mode 100644 index 000000000000..8f9d4f0c92c2 --- /dev/null +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -0,0 +1,50 @@ +package com.thealgorithms.misc; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class FourSumProblem { + public List> fourSum(int[] nums, int target) { + int n = nums.length; + List> ans = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < n; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + for (int j = i + 1; j < n; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; + } + + int k = j + 1; + int l = n - 1; + + while (k < l) { + long sum = (long) nums[i] + nums[j] + nums[k] + nums[l]; + + if (sum == target) { + ans.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l])); + k++; + l--; + + while (k < l && nums[k] == nums[k - 1]) { + k++; + } + while (k < l && nums[l] == nums[l + 1]) { + l--; + } + } else if (sum < target) { + k++; + } else { + l--; + } + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java new file mode 100644 index 000000000000..8062f87a5508 --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java @@ -0,0 +1,78 @@ +package com.thealgorithms.misc; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; + +public class FourSumProblemTest { + + private final FourSumProblem fourSumProblem = new FourSumProblem(); + + @Test + public void testFourSum_WithValidInput() { + int[] nums = {1, 0, -1, 0, -2, 2}; + int target = 0; + List> expected = Arrays.asList( + Arrays.asList(-2, 0, 0, 2), + Arrays.asList(-1, 0, 0, 1) + ); + + List> result = fourSumProblem.fourSum(nums, target); + assertEquals(expected.size(), result.size()); + assertTrue(result.containsAll(expected)); + } + + @Test + public void testFourSum_WithNoSolution() { + int[] nums = {1, 2, 3, 4}; + int target = 100; + List> result = fourSumProblem.fourSum(nums, target); + assertTrue(result.isEmpty()); + } + + @Test + public void testFourSum_WithDuplicates() { + int[] nums = {1, 1, 1, 1}; + int target = 4; + List> expected = Arrays.asList( + Arrays.asList(1, 1, 1, 1) + ); + + List> result = fourSumProblem.fourSum(nums, target); + assertEquals(expected.size(), result.size()); + assertTrue(result.containsAll(expected)); + } + + @Test + public void testFourSum_WithNegativeNumbers() { + int[] nums = {-3, -2, -1, 0, 0, 1, 2, 3}; + int target = 0; + List> expected = Arrays.asList( + Arrays.asList(-3, 1, 1, 1), + Arrays.asList(-2, 0, 0, 2), + Arrays.asList(-1, 0, 0, 1) + ); + + List> result = fourSumProblem.fourSum(nums, target); + assertEquals(expected.size(), result.size()); + assertTrue(result.containsAll(expected)); + } + + @Test + public void testFourSum_WithEmptyArray() { + int[] nums = {}; + int target = 0; + List> result = fourSumProblem.fourSum(nums, target); + assertTrue(result.isEmpty()); + } + + @Test + public void testFourSum_WithLessThanFourElements() { + int[] nums = {1, 2, 3}; + int target = 6; + List> result = fourSumProblem.fourSum(nums, target); + assertTrue(result.isEmpty()); + } +}