packagecom.thealgorithms.others;importjava.util.Arrays;importjava.util.Scanner;classThreeSum{publicstaticvoidmain(String args[]){Scanner sc =newScanner(System.in);int n = sc.nextInt();// Length of an arrayint a[]=newint[n];for(int i =0; i < n; i++){
a[i]= sc.nextInt();}System.out.println("Target");int n_find = sc.nextInt();Arrays.sort(a);// Sort the array if array is not sortedfor(int i =0; i < n; i++){int l = i +1, r = n -1;while(l < r){if(a[i]+ a[l]+ a[r]== n_find){System.out.println(a[i]+" "+ a[l]+" "+ a[r]);break;}// if you want all the triplets write l++;r--; insted of break;elseif(a[i]+ a[l]+ a[r]< n_find){
l++;}else{
r--;}}}
sc.close();}}