LeetCode 18. 4Sum



Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.


The solution set must not contain duplicate quadruplets.



For 4 sum, we need to find 3 other numbers add with nums[i] to equal to the target.

Then it becomes the 3 sum problem.

For 3 sum, we need to find 2 other numbers add with nums[j] to equal to the target – nums[i].

Then it becomes the 2 sum problem.

We sorted the input array. Then we can use two pointers techniques to solve.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *