18. 4Sum

Explanation:

To solve this problem, we can extend the idea of the "3Sum" problem by using a similar approach. We can sort the input array and then use three nested loops to fix the first two elements and then use two pointers to find the other two elements. By avoiding duplicates during the iteration and adjusting the pointers based on the target sum, we can find all unique quadruplets that sum up to the target.

  • Sort the input array.
  • Iterate over the array with the first pointer.
  • Inside the first loop, iterate over the array with the second pointer.
  • Inside the second loop, use two pointers (left and right) to find the other two elements that sum up to the target.
  • Skip duplicates to avoid duplicate quadruplets.
  • Adjust the pointers based on the sum compared to the target.
  • Continue until all unique quadruplets are found.

Time Complexity: O(n^3) where n is the number of elements in the input array. The outer loop runs n times, and the inner loop runs n-1, n-2, n-3 times in subsequent iterations.

Space Complexity: O(1) as we are using only a constant amount of extra space for variables.

:

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length < 4) {
            return result;
        }
        Arrays.sort(nums);
        
        for (int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                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++;
                        }
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        left++;
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        right--;
                    }
                }
            }
        }
        return result;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.