Problem

4Sum

LeetCode #18Medium
3Sum with one more index pinned

Given an array and a target, return all unique quadruples [a, b, c, d] that sum to the target. The solution set must not contain duplicate quadruples.

Asked atAmazonAdobeMeta
-2
-1
0
0
1
2
[0][1][2][3][4][5]
Brute force · four nested loops
▸
1for i < j < k < l:
2 if arr[i] + arr[j] + arr[k] + arr[l] == target: record it
3de-duplicate the results
state
  • target0

line 1Try every quadruple and keep those summing to 0. With n = 6 that is already 15 combinations.