-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriplet.java
More file actions
30 lines (30 loc) · 913 Bytes
/
triplet.java
File metadata and controls
30 lines (30 loc) · 913 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
import java.util.Scanner;
public class triplet {
static int tripletSum(int []arr,int target){
int ans=0;
int n=arr.length;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
if(arr[i]+arr[j]+arr[k]==target){
ans++;
}
}
}
}
return ans;
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("enter array size:");
int n=sc.nextInt();
int []arr=new int[n];
System.out.println("enter" +n+ "elements");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
System.out.println("enter target sum");
int target=sc.nextInt();
System.out.println(tripletSum(arr,target));
}
}