-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripletsWithSumZero.java
More file actions
35 lines (33 loc) · 984 Bytes
/
TripletsWithSumZero.java
File metadata and controls
35 lines (33 loc) · 984 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
package Codes;
import java.util.HashSet;
import java.util.Scanner;
public class TripletsWithSumZero {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=sc.nextInt();
}
function(array);
}
static void function(int[] array){
boolean flag=false;
for (int i=0;i<array.length-1;i++){
HashSet<Integer> hashSet=new HashSet<>();
for (int j=i+1;j<array.length;j++){
int num=-(array[i]+array[j]);
if (hashSet.contains(num)){
// System.out.println(num+" "+array[i]+" "+array[j]);
flag=true;
}
else
hashSet.add(array[j]);
}
}
if (!flag)
System.out.println("0");
else
System.out.println(1);
}
}