diff --git a/index.html b/index.html index 41c22c9..0556680 100644 --- a/index.html +++ b/index.html @@ -457,6 +457,85 @@
+

4Sum Better

+
import java.util.*;
+
+        public class FourSumBetter {
+            public List> fourSum(int[] nums, int target) {
+                int n = nums.length;
+                Set> set = new HashSet<>();
+                for (int i = 0; i < n; i++) {
+                    for (int j = i + 1; j < n; j++) {
+                        HashSet seen = new HashSet<>();
+                        for (int k = j + 1; k < n; k++) {
+                            long sum = (long) nums[i] + nums[j] + nums[k];
+                            long needed = (long) target - sum;
+                            if (seen.contains(needed)) {
+                                List temp = Arrays.asList(nums[i], nums[j], nums[k], (int) needed);
+                                Collections.sort(temp);
+                                set.add(temp);
+                            }
+                            seen.add((long) nums[k]);
+                        }
+                    }
+                }
+                return new ArrayList<>(set);
+            }
+
+            public static void main(String[] args) {
+                FourSumBetter sol = new FourSumBetter();
+                int[] nums = {1, 0, -1, 0, -2, 2}; // Example input
+                int target = 0;
+                List> result = sol.fourSum(nums, target);
+                System.out.println("Quadruplets that sum to target: " + result);
+            }
+        }
+                
+ +

4Sum Optimal

+
import java.util.*;
+
+        public class FourSumOptimal {
+            public List> fourSum(int[] nums, int target) {
+                int n = nums.length;
+                List> ans = new ArrayList<>();
+                Arrays.sort(nums);
+                for (int i = 0; i < n - 3; i++) {
+                    if (i > 0 && nums[i] == nums[i - 1]) continue;
+                    for (int j = i + 1; j < n - 2; 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]));
+                                while (k < l && nums[k] == nums[k + 1]) k++;
+                                while (k < l && nums[l] == nums[l - 1]) l--;
+                                k++;
+                                l--;
+                            } 
+                            else if (sum < target) {
+                                k++;
+                            } 
+                            else {
+                                l--;
+                            }
+                        }
+                    }
+                }
+                return ans;
+            }
+
+            public static void main(String[] args) {
+                FourSumOptimal sol = new FourSumOptimal();
+                int[] nums = {1, 0, -1, 0, -2, 2}; // Example input
+                int target = 0;
+                List> result = sol.fourSum(nums, target);
+                System.out.println("Quadruplets that sum to target: " + result);
+            }
+        }
+                

3Sum Brute

import java.util.*;