Find Triplets with Zero Sum | 3Sum LeetCode Solution | 3Sum in Java

preview_player
Показать описание
Find triplets with zero sum. Given an array of integers, Write a code to find all unique triplets in the array which gives the sum of zero. The solution set must not contain duplicate triplets.

In this tutorial, I have explained a java code to solve 3sum problem or find triplets with zero sum.

Example 1:

Input: {-1, 0, 1, 2, -1, -4}

Output: {-1, 0, 1}, {-1, -1, 2}

Example 2:

Input: {-8, -7, 5, 2}

Output: {-7, 5, 2}
Рекомендации по теме
Комментарии
Автор

very helpful video, I was not using that condition part where i>0 and we need to continue, but now I get it why we did that.

shubhamhire
Автор

In while loop, you are only checking duplicate elements for the right side.
If I add
if(nums[left]==nums[left-1]){
left++;
continue;
}

before the first if check in while loop it gives me wrong answer.

a.yashwanth
Автор

Why did you keep end<arr.length -1 && arr[end]==arr[end+1]... I didn't get this line..

saisaranya
Автор

What about the Time Complexity when you are doing the sort ?

alxx
Автор

We can use binary search to find the last and first occurences.

weirdprogrammer
Автор

which device u used to write on the screen?

mdaamirkhan
Автор

You have checked duplicate condition only for end while doing two pointers, don't we need the same duplicate condition for start pointer as well

aakashchandwani
Автор

Code link is present in description box.

ProgrammingTutorialsM
Автор

Why are we only comparing immediate following numbers only (for both i & end)? What if the a number is repeated after a few numbers?

rahulgupta
Автор

Why u have use here list can u explain it

RahulVerma-ylbd