From 286e49bcd606f926606bedb514fb8fec3623deb1 Mon Sep 17 00:00:00 2001 From: SAIVARDHAN15 <9921004954@klu.ac.in> Date: Mon, 14 Oct 2024 11:47:13 +0530 Subject: [PATCH 1/3] Feat: Add FourSumProblem and JUnit Test --- .../thealgorithms/misc/FourSumProblem.java | 57 ++++++++++++++++ .../misc/FourSumProblemTest.java | 68 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/main/java/com/thealgorithms/misc/FourSumProblem.java create mode 100644 src/test/java/com/thealgorithms/misc/FourSumProblemTest.java 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..f9b337137bd9 --- /dev/null +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -0,0 +1,57 @@ +package com.thealgorithms.misc; +import java.util.*; + +public class FourSumProblem { + + /** + * The function "fourSumProblem" takes an array of integers and a target integer as input, and returns a list + * of unique quadruplets (four elements) from the array that sum up to the target value. + * It avoids duplicate quadruplets by sorting the array and skipping repeated numbers. + * The implementation uses a two-pointer approach to efficiently find the quadruplets. + * + * @param arr An array of integers. + * @param target The target sum that the four integers in each quadruplet should add up to. + * @return A list of lists, where each inner list contains four integers that sum up to the target value. + * @author saivardhan (https://github.com/saivardhan15) + */ + + public static List> fourSumProblem(int[] arr, int target) { + List> ans = new ArrayList<>(); + if (arr.length < 4) { + return ans; + } + // Sort the array to make it easier to detect duplicates + Arrays.sort(arr); + + // Loop over the first two elements + for (int i = 0; i < arr.length - 3; i++) { + if (i > 0 && arr[i] == arr[i - 1]) { // Skip duplicate numbers for 'i' + continue; + } + for (int j = i + 1; j < arr.length - 2; j++) { + if (j > i + 1 && arr[j] == arr[j - 1]) { // Skip duplicate numbers for 'j' + continue; + } + + // Two-pointer technique for the remaining two elements + int left = j + 1; + int right = arr.length - 1; + while (left < right) { + int sum = arr[i] + arr[j] + arr[left] + arr[right]; + if (sum == target) { + ans.add(Arrays.asList(arr[i], arr[j], arr[left], arr[right])); + while (left < right && arr[left] == arr[left + 1]) left++; // Skip duplicates for 'left' + while (left < right && arr[right] == arr[right - 1]) right--; // Skip duplicates for 'right' + left++; + right--; + } else if (sum < target) { + left++; + } else { + right--; + } + } + } + } + return ans; + } +} 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..bb926b3ead02 --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java @@ -0,0 +1,68 @@ +package com.thealgorithms.misc; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class FourSumProblemTest { + @Test + public void testEmptyArray() { + int[] arr = {}; + int target = 10; + List> result = FourSumProblem.fourSumProblem(arr, target); + assertTrue(result.isEmpty(), "Expected no quadruplets for an empty array"); + } + + @Test + public void testLessThanFourElements() { + int[] arr = {1, 2, 3}; + int target = 6; + List> result = FourSumProblem.fourSumProblem(arr, target); + assertTrue(result.isEmpty(), "Expected no quadruplets when array has less than 4 elements"); + } + + @Test + public void testNoValidQuadruplet() { + int[] arr = {1, 2, 3, 4, 5}; + int target = 100; + List> result = FourSumProblem.fourSumProblem(arr, target); + assertTrue(result.isEmpty(), "Expected no quadruplets when no combination matches the target"); + } + + @Test + public void testValidQuadruplet() { + int[] arr = {1, 0, -1, 0, -2, 2}; + int target = 0; + List> result = FourSumProblem.fourSumProblem(arr, target); + + List> expected = Arrays.asList(Arrays.asList(-2, -1, 1, 2), Arrays.asList(-2, 0, 0, 2), Arrays.asList(-1, 0, 0, 1)); + assertEquals(expected.size(), result.size(), "Expected 3 valid quadruplets"); + assertTrue(result.containsAll(expected), "The result should contain all expected quadruplets"); + } + + @Test + public void testWithDuplicates() { + int[] arr = {2, 2, 2, 2, 2}; + int target = 8; + List> result = FourSumProblem.fourSumProblem(arr, target); + + List> expected = Arrays.asList(Arrays.asList(2, 2, 2, 2)); + assertEquals(expected.size(), result.size(), "Expected 1 valid quadruplet for repeated numbers"); + assertTrue(result.containsAll(expected), "The result should contain the quadruplet [2, 2, 2, 2]"); + } + + @Test + public void testNegativeNumbers() { + int[] arr = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; + int target = 0; + List> result = FourSumProblem.fourSumProblem(arr, target); + + List> expected = Arrays.asList(Arrays.asList(-5, -4, 4, 5), Arrays.asList(-5, -3, 3, 5), Arrays.asList(-5, -2, 2, 5), Arrays.asList(-5, -2, 3, 4), Arrays.asList(-5, -1, 1, 5), Arrays.asList(-5, -1, 2, 4), Arrays.asList(-5, 0, 1, 4), Arrays.asList(-5, 0, 2, 3), + Arrays.asList(-4, -3, 2, 5), Arrays.asList(-4, -3, 3, 4), Arrays.asList(-4, -2, 1, 5), Arrays.asList(-4, -2, 2, 4), Arrays.asList(-4, -1, 0, 5), Arrays.asList(-4, -1, 1, 4), Arrays.asList(-4, -1, 2, 3), Arrays.asList(-4, 0, 1, 3), Arrays.asList(-3, -2, 0, 5), Arrays.asList(-3, -2, 1, 4), + Arrays.asList(-3, -2, 2, 3), Arrays.asList(-3, -1, 0, 4), Arrays.asList(-3, -1, 1, 3), Arrays.asList(-3, 0, 1, 2), Arrays.asList(-2, -1, 0, 3), Arrays.asList(-2, -1, 1, 2)); + + assertEquals(expected.size(), result.size(), "Expected 24 valid quadruplets for negative and positive numbers"); + assertTrue(result.containsAll(expected), "The result should contain all expected quadruplets"); + } +} From ae53ab5f57af48ef1c338381c527a0287be9f4a6 Mon Sep 17 00:00:00 2001 From: SAIVARDHAN15 <9921004954@klu.ac.in> Date: Mon, 14 Oct 2024 11:58:43 +0530 Subject: [PATCH 2/3] Feat: Add FourSumProblem and JUnit Test --- .../com/thealgorithms/misc/FourSumProblem.java | 14 +++++++++++--- .../com/thealgorithms/misc/FourSumProblemTest.java | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/FourSumProblem.java b/src/main/java/com/thealgorithms/misc/FourSumProblem.java index f9b337137bd9..7cecacb5f1b1 100644 --- a/src/main/java/com/thealgorithms/misc/FourSumProblem.java +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -1,5 +1,9 @@ package com.thealgorithms.misc; -import java.util.*; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class FourSumProblem { @@ -40,8 +44,12 @@ public static List> fourSumProblem(int[] arr, int target) { int sum = arr[i] + arr[j] + arr[left] + arr[right]; if (sum == target) { ans.add(Arrays.asList(arr[i], arr[j], arr[left], arr[right])); - while (left < right && arr[left] == arr[left + 1]) left++; // Skip duplicates for 'left' - while (left < right && arr[right] == arr[right - 1]) right--; // Skip duplicates for 'right' + while (left < right && arr[left] == arr[left + 1]){ // Skip duplicates for 'left' + left++; + } + while (left < right && arr[right] == arr[right - 1]){ // Skip duplicates for 'right' + right--; + } left++; right--; } else if (sum < target) { diff --git a/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java index bb926b3ead02..aea9760f26fa 100644 --- a/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java +++ b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java @@ -1,6 +1,6 @@ package com.thealgorithms.misc; -import static org.junit.jupiter.api.Assertions.*; - +import git static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; From de93f167239c82e97fc0d4740ea38efd03c3ad4e Mon Sep 17 00:00:00 2001 From: SAIVARDHAN15 <9921004954@klu.ac.in> Date: Mon, 14 Oct 2024 12:00:11 +0530 Subject: [PATCH 3/3] Feat: Add FourSumProblem and JUnit Test --- src/main/java/com/thealgorithms/misc/FourSumProblem.java | 5 ++--- src/test/java/com/thealgorithms/misc/FourSumProblemTest.java | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/FourSumProblem.java b/src/main/java/com/thealgorithms/misc/FourSumProblem.java index 7cecacb5f1b1..aabcb88ec3f7 100644 --- a/src/main/java/com/thealgorithms/misc/FourSumProblem.java +++ b/src/main/java/com/thealgorithms/misc/FourSumProblem.java @@ -1,6 +1,5 @@ package com.thealgorithms.misc; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -44,10 +43,10 @@ public static List> fourSumProblem(int[] arr, int target) { int sum = arr[i] + arr[j] + arr[left] + arr[right]; if (sum == target) { ans.add(Arrays.asList(arr[i], arr[j], arr[left], arr[right])); - while (left < right && arr[left] == arr[left + 1]){ // Skip duplicates for 'left' + while (left < right && arr[left] == arr[left + 1]) { // Skip duplicates for 'left' left++; } - while (left < right && arr[right] == arr[right - 1]){ // Skip duplicates for 'right' + while (left < right && arr[right] == arr[right - 1]) { // Skip duplicates for 'right' right--; } left++; diff --git a/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java index aea9760f26fa..9e4328c06ca4 100644 --- a/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java +++ b/src/test/java/com/thealgorithms/misc/FourSumProblemTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.misc; import git static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test;