Given an array *S* of *n* integers, are there elements *a*, *b*, *c* in *S* such that *a* + *b* + *c* = 0? Find all unique triplets in the array which gives the sum of zero.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:

[

[-1, 0, 1],

[-1, -1, 2]

]

@Test

public void test3Sum()

{

int []s={-1,0,1,2,-1,-4};

int temp=0;

ArrayList<String> list=new ArrayList<String>();

for(int i=0;i<s.length-1;i++)

{

temp=s[i];

for(int j=i+1;j<s.length-1;j++)

{

if(temp+s[j]+s[j+1]==0)

{

list.add(temp+”,”+s[j]+”,”+s[j+1]);

}

}//end of for

}

for(String i:list)

{

System.out.println(i);

}

}

### Like this:

Like Loading...

*Related*

## About qainterviews

Software Geek,QA Expert,Blogger