Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Has3sumTest {
- public static void main(String[] args) throws IOException {
- BufferedReader jin = new BufferedReader(
- new InputStreamReader(System.in));
- int n = Integer.parseInt(jin.readLine());
- String nums[] = jin.readLine().split("\\s+");
- int a[] = new int[n];
- for (int i = 0; i < n; ++i)
- a[i] = Integer.parseInt(nums[i]);
- System.out.println(has3sum(a));
- }
- public static boolean has3sum(int a[]) {
- if(a.length<3) return false;
- Set<Integer> set1 = new HashSet<Integer>();
- for(int i=0;i<a.length;i++){
- set1.add(a[i]);
- }
- Iterator<Integer> set1it = set1.iterator();
- Iterator<Integer> set2it = set1.iterator();
- set2it.next();
- while(set1it.hasNext()){
- int br1 = set1it.next();
- while(set2it.hasNext()){
- int br2 = set2it.next();
- if(set1.contains(-br1-br2)){
- return true;
- }
- }
- set2it = set1.iterator();
- }
- return false;
- }
- public static boolean has3sumtree(int a[]) {
- if(a.length<3) return false;
- Set<Integer> set1 = new TreeSet<Integer>();
- for(int i=0;i<a.length;i++){
- set1.add(a[i]);
- }
- Iterator<Integer> set1it = set1.iterator();
- Iterator<Integer> set2it = set1.iterator();
- set2it.next();
- while(set1it.hasNext()){
- int br1 = set1it.next();
- while(set2it.hasNext()){
- int br2 = set2it.next();
- if(set1.contains(-br1-br2)){
- return true;
- }
- }
- set2it = set1.iterator();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement