Problem 18: Four Sum
Question
LeetCode Link | 18. Four Sum | Medium
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
, andd
are distinct.nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Constrains
1 <= nums.length <= 200
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9
Examples
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]]
Solution Approach
Method: Two-pointers
Constrains
- Unique
If this question allow duplicate triplets, this could be solve using hash method by finding if other two values sum up equal to 0 - (a + b)
which just like question 454. Four Sum II.
Concept Explanation
The solution for “4 Sum”, similar to the problem 15. 3 Sum, which employs the two-pointer technique. The basic approach for this question is to add an additional for loop on top of the “3 Sum” solution.
The two-pointer method for this question involves two layers of for loops, with nums[i]
and nums[j]
as fixed values, and still utilizes the lo
and hi
indices as two pointers within the loop to find combinations where nums[i] + nums[j] + nums[lo] + nums[hi] == target
. The time complexity for “3 Sum” is 𝑂(𝑛2), and for “4 Sum” it increases to 𝑂(𝑛3).
Similarly, the same approach can be applied to “5 Sum”, “6 Sum”, and so on……
For problem 15, “3 Sum”, the two-pointer method reduces the original brute force solution from 𝑂(𝑛3) to 𝑂(𝑛2). For “4 Sum”, the two-pointer method reduces the brute force solution from 𝑂(𝑛4) to 𝑂(𝑛3).
Code
- Time complexity: O(n3)
- Space complexity: O(1)
TypeScript
1 | function fourSum(nums: number[], target: number): number[][] { |
Conclusion
When dealing this type of question, think if it is the best way to use hash method even it looks like a hash method problem. Also, consider all possible duplicates before you actually starting write the code.