|
| 1 | +package com.thealgorithms.misc; |
| 2 | + |
1 | 3 | import java.util.ArrayList;
|
2 | 4 | import java.util.Arrays;
|
3 | 5 | import java.util.List;
|
4 | 6 |
|
5 |
| -public class fourSum { |
6 |
| - public static List<List<Integer>> fourSum(int[] nums, int target) { |
| 7 | +public class FourSum { // Class name changed |
| 8 | + // Method name changed to findFourSum |
| 9 | + public static List<List<Integer>> findFourSum(int[] nums, int target) { |
7 | 10 | List<List<Integer>> result = new ArrayList<>();
|
8 |
| - if (nums == null || nums.length < 4) return result; |
| 11 | + if (nums == null || nums.length < 4) { |
| 12 | + return result; // Added curly braces for 'if' |
| 13 | + } |
9 | 14 |
|
10 | 15 | Arrays.sort(nums); // Sort the array first
|
11 | 16 |
|
12 | 17 | for (int i = 0; i < nums.length - 3; i++) {
|
13 |
| - if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates |
| 18 | + if (i > 0 && nums[i] == nums[i - 1]) { |
| 19 | + continue; // Added curly braces for 'if' |
| 20 | + } |
14 | 21 | for (int j = i + 1; j < nums.length - 2; j++) {
|
15 |
| - if (j > i + 1 && nums[j] == nums[j - 1]) continue; // Skip duplicates |
| 22 | + if (j > i + 1 && nums[j] == nums[j - 1]) { |
| 23 | + continue; // Added curly braces for 'if' |
| 24 | + } |
16 | 25 | int left = j + 1, right = nums.length - 1;
|
17 | 26 | while (left < right) {
|
18 | 27 | int sum = nums[i] + nums[j] + nums[left] + nums[right];
|
19 | 28 | if (sum == target) {
|
20 | 29 | result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
|
21 |
| - while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates |
22 |
| - while (left < right && nums[right] == nums[right - 1]) right--; // Skip duplicates |
| 30 | + while (left < right && nums[left] == nums[left + 1]) { |
| 31 | + left++; // Added curly braces for 'while' |
| 32 | + } |
| 33 | + while (left < right && nums[right] == nums[right - 1]) { |
| 34 | + right--; // Added curly braces for 'while' |
| 35 | + } |
23 | 36 | left++;
|
24 | 37 | right--;
|
25 | 38 | } else if (sum < target) {
|
26 |
| - left++; |
| 39 | + left++; // Added curly braces for 'if' |
27 | 40 | } else {
|
28 |
| - right--; |
| 41 | + right--; // Added curly braces for 'else' |
29 | 42 | }
|
30 | 43 | }
|
31 | 44 | }
|
32 | 45 | }
|
33 | 46 | return result;
|
34 | 47 | }
|
35 | 48 |
|
| 49 | + // Private constructor to prevent instantiation (utility class) |
| 50 | + private FourSum() {} |
| 51 | + |
36 | 52 | public static void main(String[] args) {
|
37 | 53 | int[] arr1 = {1, 0, -1, 0, -2, 2};
|
38 | 54 | int target1 = 0;
|
39 |
| - System.out.println(fourSum(arr1, target1)); |
| 55 | + System.out.println(findFourSum(arr1, target1)); // Updated method call |
40 | 56 |
|
41 | 57 | int[] arr2 = {4, 3, 3, 4, 4, 2, 1, 2, 1, 1};
|
42 | 58 | int target2 = 9;
|
43 |
| - System.out.println(fourSum(arr2, target2)); |
| 59 | + System.out.println(findFourSum(arr2, target2)); // Updated method call |
44 | 60 | }
|
45 | 61 | }
|
0 commit comments