forked from suchimaheshwari/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrays:Triplet Sum
52 lines (36 loc) · 1.81 KB
/
Arrays:Triplet Sum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Solution{
public static void tripletSum(int[] input, int x){
for(int i=0;i<input.length;i++)
{
for(int j=i+1;j<input.length;j++)
{
for(int k=j+1;k<input.length;k++)
{
if(input[i]+input[j]+input[k] == x){
if(input[i]<=input[j] && input[i]<=input[k]){
System.out.print(input[i]+" ");
if (input[j]<=input[k])
System.out.println(input[j]+" "+input[k]);
else
System.out.println(input[k]+" "+input[j]);
}
else
if(input[j]<=input[i] && input[j]<=input[k]){
System.out.print(input[j]+" ");
if (input[i]<=input[k])
System.out.println(input[i]+" "+input[k]);
else
System.out.println(input[k]+" "+input[i]);
}
else{
System.out.print(input[k]+" ");
if (input[i]<=input[j])
System.out.println(input[i]+" "+input[j]);
else
System.out.println(input[j]+" "+input[i]);
}
}
}
}
}
}}