0% found this document useful (0 votes)
2 views1 page

3 Sum 1

Uploaded by

Bunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

3 Sum 1

Uploaded by

Bunny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;
class TUF {
static ArrayList < ArrayList < Integer >> threeSum(int nums[]) {
ArrayList < ArrayList < Integer >> ans = new ArrayList < > ();

int i, j, k;
for (i = 0; i < nums.length - 2; i++) {
for (j = i + 1; j < nums.length - 1; j++) {
for (k = j + 1; k < nums.length; k++) {
ArrayList < Integer > temp = new ArrayList < > ();
if (nums[i] + nums[j] + nums[k] == 0) {
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[k]);
}
if (temp.size() != 0)
ans.add(temp);
}
}
}

return ans;
}

public static void main(String args[]) {


int arr[]={-1,0,1,2,-1,-4};
ArrayList < ArrayList < Integer >> ans;
ans = threeSum(arr);
System.out.println("The triplets are as follows: ");
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans.get(i).size(); j++) {
System.out.print(ans.get(i).get(j) + " ");
}
System.out.println();
}

}
}

You might also like