From d29a467aff66514ec280afa6b8355cf929a54172 Mon Sep 17 00:00:00 2001 From: kkn2k19 Date: Sun, 20 Oct 2024 00:53:53 +0530 Subject: [PATCH 1/3] Added 4-Sum Problem implementation --- .../java/com/thealgorithms/misc/fourSum.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/com/thealgorithms/misc/fourSum.java diff --git a/src/test/java/com/thealgorithms/misc/fourSum.java b/src/test/java/com/thealgorithms/misc/fourSum.java new file mode 100644 index 000000000000..37ac025cd242 --- /dev/null +++ b/src/test/java/com/thealgorithms/misc/fourSum.java @@ -0,0 +1,50 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class fourSum { + public static List> fourSum(int[] nums, int target) { + List> result = new ArrayList<>(); + if (nums == null || nums.length < 4) + return result; + + Arrays.sort(nums); // Sort the array first + + for (int i = 0; i < nums.length - 3; i++) { + if (i > 0 && nums[i] == nums[i - 1]) + continue; // Skip duplicates + for (int j = i + 1; j < nums.length - 2; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) + continue; // Skip duplicates + int left = j + 1, right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[j] + nums[left] + nums[right]; + if (sum == target) { + result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); + while (left < right && nums[left] == nums[left + 1]) + left++; // Skip duplicates + while (left < right && nums[right] == nums[right - 1]) + right--; // Skip duplicates + left++; + right--; + } else if (sum < target) { + left++; + } else { + right--; + } + } + } + } + return result; + } + + public static void main(String[] args) { + int[] arr1 = { 1, 0, -1, 0, -2, 2 }; + int target1 = 0; + System.out.println(fourSum(arr1, target1)); + + int[] arr2 = { 4, 3, 3, 4, 4, 2, 1, 2, 1, 1 }; + int target2 = 9; + System.out.println(fourSum(arr2, target2)); + } +} From a4d91c6e447482e39ac0d900e5cdae5d35c49346 Mon Sep 17 00:00:00 2001 From: kkn2k19 Date: Sun, 20 Oct 2024 21:53:26 +0530 Subject: [PATCH 2/3] Applied clang-format --- .../java/com/thealgorithms/misc/fourSum.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/fourSum.java b/src/test/java/com/thealgorithms/misc/fourSum.java index 37ac025cd242..8903e7bac305 100644 --- a/src/test/java/com/thealgorithms/misc/fourSum.java +++ b/src/test/java/com/thealgorithms/misc/fourSum.java @@ -5,26 +5,21 @@ public class fourSum { public static List> fourSum(int[] nums, int target) { List> result = new ArrayList<>(); - if (nums == null || nums.length < 4) - return result; + if (nums == null || nums.length < 4) return result; Arrays.sort(nums); // Sort the array first for (int i = 0; i < nums.length - 3; i++) { - if (i > 0 && nums[i] == nums[i - 1]) - continue; // Skip duplicates + if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates for (int j = i + 1; j < nums.length - 2; j++) { - if (j > i + 1 && nums[j] == nums[j - 1]) - continue; // Skip duplicates + if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates int left = j + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); - while (left < right && nums[left] == nums[left + 1]) - left++; // Skip duplicates - while (left < right && nums[right] == nums[right - 1]) - right--; // Skip duplicates + while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates + while (left < right && nums[right] == nums[right - 1]) right--; // Skip duplicates left++; right--; } else if (sum < target) { @@ -39,11 +34,11 @@ public static List> fourSum(int[] nums, int target) { } public static void main(String[] args) { - int[] arr1 = { 1, 0, -1, 0, -2, 2 }; + int[] arr1 = {1, 0, -1, 0, -2, 2}; int target1 = 0; System.out.println(fourSum(arr1, target1)); - int[] arr2 = { 4, 3, 3, 4, 4, 2, 1, 2, 1, 1 }; + int[] arr2 = {4, 3, 3, 4, 4, 2, 1, 2, 1, 1}; int target2 = 9; System.out.println(fourSum(arr2, target2)); } From 54fc57fc083dc330f47515d78d71b6504b2c114e Mon Sep 17 00:00:00 2001 From: kkn2k19 Date: Sun, 20 Oct 2024 22:42:03 +0530 Subject: [PATCH 3/3] Resolved Checkstyle violations --- .../java/com/thealgorithms/misc/fourSum.java | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/misc/fourSum.java b/src/test/java/com/thealgorithms/misc/fourSum.java index 8903e7bac305..01199fec371a 100644 --- a/src/test/java/com/thealgorithms/misc/fourSum.java +++ b/src/test/java/com/thealgorithms/misc/fourSum.java @@ -1,31 +1,44 @@ +package com.thealgorithms.misc; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class fourSum { - public static List> fourSum(int[] nums, int target) { +public class FourSum { // Class name changed + // Method name changed to findFourSum + public static List> findFourSum(int[] nums, int target) { List> result = new ArrayList<>(); - if (nums == null || nums.length < 4) return result; + if (nums == null || nums.length < 4) { + return result; // Added curly braces for 'if' + } Arrays.sort(nums); // Sort the array first for (int i = 0; i < nums.length - 3; i++) { - if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates + if (i > 0 && nums[i] == nums[i - 1]) { + continue; // Added curly braces for 'if' + } for (int j = i + 1; j < nums.length - 2; j++) { - if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; // Added curly braces for 'if' + } int left = j + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); - while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates - while (left < right && nums[right] == nums[right - 1]) right--; // Skip duplicates + while (left < right && nums[left] == nums[left + 1]) { + left++; // Added curly braces for 'while' + } + while (left < right && nums[right] == nums[right - 1]) { + right--; // Added curly braces for 'while' + } left++; right--; } else if (sum < target) { - left++; + left++; // Added curly braces for 'if' } else { - right--; + right--; // Added curly braces for 'else' } } } @@ -33,13 +46,16 @@ public static List> fourSum(int[] nums, int target) { return result; } + // Private constructor to prevent instantiation (utility class) + private FourSum() {} + public static void main(String[] args) { int[] arr1 = {1, 0, -1, 0, -2, 2}; int target1 = 0; - System.out.println(fourSum(arr1, target1)); + System.out.println(findFourSum(arr1, target1)); // Updated method call int[] arr2 = {4, 3, 3, 4, 4, 2, 1, 2, 1, 1}; int target2 = 9; - System.out.println(fourSum(arr2, target2)); + System.out.println(findFourSum(arr2, target2)); // Updated method call } }