diff --git a/dsa/4sumBetter.java b/dsa/4sumBetter.java new file mode 100644 index 0000000..8e3748f --- /dev/null +++ b/dsa/4sumBetter.java @@ -0,0 +1,32 @@ +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); + } +} diff --git a/dsa/4sumOptimal.java b/dsa/4sumOptimal.java new file mode 100644 index 0000000..187589b --- /dev/null +++ b/dsa/4sumOptimal.java @@ -0,0 +1,42 @@ +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); + } +} diff --git a/dsa/LargestSubarrayWith0Sum.java b/dsa/LargestSubarrayWith0Sum.java new file mode 100644 index 0000000..e69de29 diff --git a/index.html b/index.html index 0556680..8001c0e 100644 --- a/index.html +++ b/index.html @@ -491,6 +491,28 @@

4Sum Better

} } +

Largest Subarray With 0 Sum - Brute Approach

+
class Solution {
+            int maxLength(int arr[]) {
+                int n = arr.length;
+                int maxLen = 0; 
+                // This will store the answer
+                for(int i = 0; i < n; i++) {
+                    int sum = 0;       // IMPORTANT: Reset sum for every new starting index i
+                    for(int j = i; j < n; j++) {     // Fixed: j < n (not i < n)
+                      sum+= arr[j];
+                      if(sum == 0){
+                          int currL = j-i+1;
+                          if(currL > maxLen){
+                              maxLen = currL;
+                          }
+                      }
+                    }
+                }
+                return maxLen;         // Don't forget to return the result
+            }
+        }
+                

4Sum Optimal

import java.util.*;