Given a random integer array and a number x. Find and print the triplets of elements in the array which sum to x. While printing a triplet, print the smallest element first. That is, if a valid triplet is (6, 5, 10) print "5 6 10". There is no constraint that out of 5 triplets which have to be printed on 1st line. You can print triplets in any order, just be careful about the order of elements in a triplet.
import java.util.Arrays;
public class TripletSum {
public static void FindTriplet(int[] arr, int x){
/* Your class should be named TripletSum.
* Don't write main().
* Don't read input, it is passed as function argument.
* Print output and don't return it.
* Taking input is handled automatically.
*/
Arrays.sort(arr);
int b=0, c=0;
for(int a=0; a<arr.length; a++){
b=a+1; c=b+1;
if((arr[a]+arr[b]+arr[c])==x){
System.out.print(a+"");
System.out.print(b+"");
System.out.print(c+"");
}
}
}
}