4Sum - Leetcode Solution


💡 Step-by-Step Thought Process

  1. Understand the problem: Find all unique quadruplets in an array that sum to a given target.
  2. Sort the array to facilitate two-pointer traversal and handle duplicates.
  3. Initialize an empty list to store the quadruplets.
  4. Iterate through index i from 0 to n-4.
  5. Skip duplicate values at i to avoid redundant quadruplets.
  6. For each i, iterate through index j from i+1 to n-3.
  7. Skip duplicate values at j to avoid redundant quadruplets.
  8. Use two pointers, lo (j+1) and hi (n-1), to find pairs that sum with nums[i] and nums[j] to the target.
  9. Compute the sum as nums[i] + nums[j] + nums[lo] + nums[hi].
  10. If the sum equals the target, add the quadruplet to the answer list, increment lo, decrement hi, and skip duplicates for lo and hi.
  11. If the sum is less than target, increment lo; otherwise, decrement hi.
  12. Return the list of quadruplets.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the 4Sum Problem

The 4Sum problem challenges you to find all unique quadruplets in an array that sum to a specific target. Unlike the simpler 2Sum and 3Sum problems, this version requires considering four elements, making efficiency and duplicate handling critical. The key is to reduce the time complexity from O(n⁴) using nested loops to a more optimal combination of sorting and two-pointer strategies.

Efficient Strategy with Sorting and Two Pointers

To solve the 4Sum problem effectively, begin by sorting the input array. Sorting enables us to identify and skip duplicate combinations easily, which is crucial for generating unique quadruplets. After sorting, we fix two numbers using two nested loops, and for each such pair, we use the two-pointer approach to find the remaining two numbers that complete the quadruplet.

Specifically, we fix the first index i and the second index j. Then, we initialize two pointers: lo to j + 1 and hi to the end of the array. We compute the current sum of the four selected elements. If it matches the target, we add the combination to the result and skip over duplicates. If the sum is less than the target, we move the lo pointer forward. If it's greater, we move the hi pointer backward.

To avoid repeating the same quadruplets, we skip over duplicate values for i, j, lo, and hi. This ensures the result only includes unique combinations, as required by the problem.

Time and Space Complexity

  • Time Complexity: O(n³), where n is the number of elements in the input array. Although we use four indices, the two-pointer technique reduces one loop, making it significantly more efficient than O(n⁴).
  • Space Complexity: O(1) additional space, assuming the result list is not counted; otherwise, it grows with the number of valid quadruplets.

Why This Method Works

This strategy works because sorting brings structure to the problem, allowing the use of the two-pointer method, which is highly efficient in reducing unnecessary computations. It also simplifies the logic for skipping duplicates. The overall flow is systematic, making it both efficient and easy to implement once understood.

Conclusion

Mastering the 4Sum problem reinforces essential algorithmic techniques such as nested iteration control, two-pointer traversal, and duplicate handling. These strategies are crucial for solving high-complexity problems in interviews and real-world applications involving search and combination generation in sorted arrays.

Get Personalized Support at AlgoMap Bootcamp 💡