4Sum - Complete Solution Guide
4Sum is LeetCode problem 18, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a , b , c , and d are distinct . nums[a] + nums[b] + nums[c] + nums[d] == target You may return the answer in any order . Example 1: Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] Example 2: Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]] Constraints: 1 <= nums.length <= 200 -10 9 <= nums[i] <= 10 9 -
Detailed Explanation
The problem requires finding all unique quadruplets (sets of four numbers) within a given array `nums` that sum up to a specified `target` value. The quadruplets must consist of distinct elements from the input array, and the order of elements within a quadruplet, as well as the order of quadruplets in the output, does not matter. The constraints include the size of the array (1 <= n <= 200) and the range of the numbers in the array and the target (-10^9 <= nums[i], target <= 10^9).
Solution Approach
The provided solution employs a nested loop approach combined with the two-pointer technique to find the quadruplets. First, the input array is sorted. The outer two loops iterate through all possible pairs of numbers. Inside these loops, the two-pointer technique is used to find two additional numbers that, when added to the first two, sum up to the target. The code also includes several optimizations, such as skipping duplicate elements and early pruning to avoid unnecessary computations.
Step-by-Step Algorithm
- Step 1: Sort the input array `nums` in ascending order.
- Step 2: Iterate through the array using the first pointer `i` from 0 to n-4.
- Step 3: Inside the first loop, skip duplicate values of `nums[i]` to avoid duplicate quadruplets.
- Step 4: Perform early pruning: check if the minimum possible sum (using `nums[i]` and the next three elements) is greater than the target or if the maximum possible sum (using `nums[i]` and the last three elements) is less than the target. If either condition is true, skip the current iteration.
- Step 5: Iterate through the array using the second pointer `j` from `i+1` to n-3.
- Step 6: Inside the second loop, skip duplicate values of `nums[j]` to avoid duplicate quadruplets.
- Step 7: Perform early pruning similar to step 4, but considering `nums[i]` and `nums[j]`.
- Step 8: Initialize two pointers, `left` (pointing to `j+1`) and `right` (pointing to `n-1`).
- Step 9: While `left < right`, calculate the `current_sum` of `nums[i]`, `nums[j]`, `nums[left]`, and `nums[right]`.
- Step 10: If `current_sum == target`, add the quadruplet to the result list, increment `left`, decrement `right`, and skip any duplicate values of `nums[left]` and `nums[right]` to avoid duplicate quadruplets.
- Step 11: If `current_sum < target`, increment `left` to increase the sum.
- Step 12: If `current_sum > target`, decrement `right` to decrease the sum.
- Step 13: Return the list of unique quadruplets.
Key Insights
- Insight 1: Sorting the array enables efficient searching for quadruplets and helps avoid duplicate solutions using the two-pointer technique.
- Insight 2: The two-pointer technique, applied after fixing the first two numbers of the quadruplet, allows finding the remaining two numbers that satisfy the target sum in O(n) time.
- Insight 3: Early pruning using checks on minimum and maximum possible sums involving the current elements can significantly reduce the search space.
Complexity Analysis
Time Complexity: O(n^3)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers, Sorting.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Cloudflare, DE Shaw, DoorDash, Google, Infosys, Meta, Microsoft, Rubrik, Samsung, ServiceNow, TikTok, Uber, Yahoo, Zoox, oyo, tcs.