Four Sum Problem

Four Sum Problem — ExecCode Easy DSA Practice

Solve the Four Sum problem on ExecCode. Free online easy DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Find all unique quadruplets that sum to target. Sort the array, fix two indices, then solve the remaining pair with left and right pointers while skipping duplicates.

Examples

Input nums = [1,0,-1,0,-2,2], target = 0; Output [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]. Input nums = [2,2,2,2,2], target = 8; Output [[2,2,2,2]]

Constraints

1 ≤ nums.length ≤ 200 -10⁹ ≤ nums[i] ≤ 10⁹ -10⁹ ≤ target ≤ 10⁹

Practice Four Sum free on ExecCode. Browse DSA problems, topic map, and placement guides.