-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrippletSum.java
More file actions
43 lines (40 loc) · 819 Bytes
/
TrippletSum.java
File metadata and controls
43 lines (40 loc) · 819 Bytes
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
import java.util.Arrays;
public class TrippletSum
{
static void tripplet(int[] arr, int sum)
{
int count=0;
Arrays.sort(arr);
for(int i=0; i<arr.length-2; i++)
{
/*for(int j=i+1; j<arr.length-1; j++)
{
for(int k=j+1; k<arr.length; k++)
{
if(arr[i]+arr[j]+arr[k] < sum)
count++;
System.out.println(arr[i]+","+arr[j]+","+arr[k]);
}
}*/
int j=i+1;
int k=arr.length-1;
while(j<k)
{
if(arr[i]+arr[j]+arr[k] < sum)
{
count += (k - j);
j++;
}
else
k--;
}
}
System.out.println("Tripplets having less then sum are:- "+count);
}
public static void main(String[] args)
{
int[] arr = {5,1,3,4,7}; //5,1,3,4,7 -2,0,1,3
int sum = 12;
tripplet(arr, sum);
}
}