diff --git a/src/test/java/com/thealgorithms/misc/Add_4_Sum_Problem.java b/src/test/java/com/thealgorithms/misc/Add_4_Sum_Problem.java new file mode 100644 index 000000000000..cc43ef7f724b --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/Add_4_Sum_Problem.java @@ -0,0 +1,67 @@ +import java.util.*; + +public class FourSum { + public static List> fourSum(int[] arr, int target) { + List> result = new ArrayList<>(); + if (arr == null || arr.length < 4) return result; + + Arrays.sort(arr); + int n = arr.length; + + + for (int i = 0; i < n - 3; i++) { + if (i > 0 && arr[i] == arr[i - 1]) continue; // Skip duplicates. + + for (int j = i + 1; j < n - 2; j++) { + if (j > i + 1 && arr[j] == arr[j - 1]) continue; // Skip duplicates. + + int left = j + 1; + int right = n - 1; + + while (left < right) { + int sum = arr[i] + arr[j] + arr[left] + arr[right]; + + if (sum == target) { + result.add(Arrays.asList(arr[i], arr[j], arr[left], arr[right])); + + + while (left < right && arr[left] == arr[left + 1]) left++; + while (left < right && arr[right] == arr[right - 1]) right--; + + left++; + right--; + } else if (sum < target) { + left++; + } else { + right--; + } + } + } + } + return result; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the number of elements in the array: "); + int n = scanner.nextInt(); + int[] arr = new int[n]; + + System.out.println("Enter the elements of the array:"); + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + System.out.print("Enter the target value: "); + int target = scanner.nextInt(); + + List> result = fourSum(arr, target); + System.out.println("Unique quadruplets that sum to " + target + ":"); + for (List quad : result) { + System.out.println(quad); + } + + scanner.close(); + } +}