fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt();
  15. int arr[] = new int[n];
  16. for(int i=0; i<n; i++){
  17. arr[i] = sc.nextInt();
  18. }
  19. int target = sc.nextInt();
  20.  
  21. HashSet<Integer> set = new HashSet<>();
  22. boolean result = false;
  23. for(int i=0; i<n; i++){
  24. int val = target-arr[i];
  25. if(set.contains(val)){
  26. result = true;
  27. System.out.println("Pair : "+"{"+val+","+arr[i]+"}");
  28. }
  29. set.add(arr[i]);
  30. }
  31.  
  32. System.out.println(result);
  33.  
  34. }
  35. }
Success #stdin #stdout 0.18s 60872KB
stdin
5
0 -1 2 -3 1
-2
stdout
Pair : {-3,1}
true