-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriplets.java
More file actions
39 lines (38 loc) · 1016 Bytes
/
triplets.java
File metadata and controls
39 lines (38 loc) · 1016 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
//Given an array of distinct integers.
//The task is to count all the triplets such that sum of two elements equals the third element.
import java.util.Scanner;
import java.util.Arrays;
public class triplets
{
static int triplets(int[] a, int n)
{
int i, j, k, count=0;
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
for(k=j+1; k<n; k++)
{
if(a[i]+a[j]==a[k] && i!=j)
{ System.out.println(a[i]+"+"+a[j]+"="+a[k]);
count++;
}
}
}
}
return count;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n, i, j, k, count;
n=s.nextInt();
int[] arr=new int[n];
for(i=0; i<n; i++)
{
arr[i]=s.nextInt();
}
Arrays.sort(arr);
count=triplets(arr, n);
System.out.println("There are "+count+" triplets.");
}
}