LeetCode 18: 4Sum Solution

Master LeetCode problem 18 (4Sum), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

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.

:

Solution Code

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;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 18 (4Sum)?

This page provides optimized solutions for LeetCode problem 18 (4Sum) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 18 (4Sum)?

The time complexity for LeetCode 18 (4Sum) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 18 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 18 in Java, C++, or Python.

Back to LeetCode Solutions