4Sum - K Sum solution explained
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
, andd
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
-109 <= nums[i] <= 109
-109 <= target <= 109
Solution:
Explanation
We will generalize 2-sum to K-Sum.
Revision of 2 Sum
There are two main ways to solve 2 Sum in $O(n)$ time. Let us look at both. We will not look at finding whether two sum exists but return all such pairs (with repitition if repititions are present). For the 2nd method, let us assume that the array was given to us in sorted order. We will talk about this assumption later.
2 Sum using HashSet: $O(n)$ time, $O(n)$ space
2 Sum using double pointer: $O(n)$ time, $O(1)$ space
Note: We had assumed nums
to be pre-sorted.
Now to generalize this to K-Sum we can have a recursive function such that KSum
for K
will be created using KSum
for K-1
. When we reach K=2
we will use the twoSum
that we have written earlier.
Now the crucial thing to note is that for K=3,4,5,...
we will always have a situation where TC
is at least $O(n^2)$, hence it makes sense to pre-sort array in $O(n\log(n))$ time and use the 2nd method. It also helps in keeping track of unique subsets because otherwise we will have to sort the subset each time before adding to res
.
Code
Complexity
TC
: $O(n^3)$
SC
: $O(n)$