304 North Cardinal St.
Dorchester Center, MA 02124

# 3Sum LeetCode Solution

## Problem – 3Sum

Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j``i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.

Notice that the solution set must not contain duplicate triplets.

Example 1:

``````Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.``````

Example 2:

``````Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.``````

Example 3:

``````Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.``````

Constraints:

• `3 <= nums.length <= 3000`
• `-105 <= nums[i] <= 105`

### 3Sum LeetCode Solution in Java

``````public List<List<Integer>> threeSum(int[] num) {
Arrays.sort(num);
for (int i = 0; i < num.length-2; i++) {
if (i == 0 || (i > 0 && num[i] != num[i-1])) {
int lo = i+1, hi = num.length-1, sum = 0 - num[i];
while (lo < hi) {
if (num[lo] + num[hi] == sum) {
while (lo < hi && num[lo] == num[lo+1]) lo++;
while (lo < hi && num[hi] == num[hi-1]) hi--;
lo++; hi--;
} else if (num[lo] + num[hi] < sum) lo++;
else hi--;
}
}
}
return res;
}``````

### 3Sum LeetCode Solution in C++

``````vector<vector<int> > threeSum(vector<int> &num) {

vector<vector<int> > res;

std::sort(num.begin(), num.end());

for (int i = 0; i < num.size(); i++) {

int target = -num[i];
int front = i + 1;
int back = num.size() - 1;

while (front < back) {

int sum = num[front] + num[back];

// Finding answer which start from number num[i]
if (sum < target)
front++;

else if (sum > target)
back--;

else {
vector<int> triplet = {num[i], num[front], num[back]};
res.push_back(triplet);

// Processing duplicates of Number 2
// Rolling the front pointer to the next different number forwards
while (front < back && num[front] == triplet[1]) front++;

// Processing duplicates of Number 3
// Rolling the back pointer to the next different number backwards
while (front < back && num[back] == triplet[2]) back--;
}

}

// Processing duplicates of Number 1
while (i + 1 < num.size() && num[i + 1] == num[i])
i++;

}

return res;

}
``````

### 3Sum LeetCode Solution in Python

``````def threeSum(self, nums):
res = []
nums.sort()
for i in xrange(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:
continue
l, r = i+1, len(nums)-1
while l < r:
s = nums[i] + nums[l] + nums[r]
if s < 0:
l +=1
elif s > 0:
r -= 1
else:
res.append((nums[i], nums[l], nums[r]))
while l < r and nums[l] == nums[l+1]:
l += 1
while l < r and nums[r] == nums[r-1]:
r -= 1
l += 1; r -= 1
return res``````
##### 3Sum LeetCode Solution Review:

In our experience, we suggest you solve this 3Sum LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the 3Sum LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this 3Sum LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions