Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
Idea is to put all the pair sums a
in hashmap along with corresponding indexes and once done check if -a
is also present in the hashmap. If both a
and -a
is present and since the question is looking for unique quadruplets then we can just filter out with indexes.
class Solution(object):
def fourSum(self, arr, target):
seen = {}
for i in range(len(arr)-1):
for j in range(i+1, len(arr)):
if arr[i]+arr[j] in seen:
seen[arr[i]+arr[j]].add((i,j))
else:
seen[arr[i]+arr[j]] = {(i,j)}
result = []
for key in seen:
if -key + target in seen:
for (i,j) in seen[key]:
for (p,q) in seen[-key + target]:
sorted_index = sorted([arr[i], arr[j], arr[p], arr[q]])
if i not in (p, q) and j not in (p, q) and sorted_index not in result:
result.append(sorted_index)
return result