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. Scanner sc = new Scanner(System.in);
  13. int n=sc.nextInt();
  14. int k=sc.nextInt();
  15. int nums[] = new int[n];
  16. for(int i=0; i<n; i++){
  17. nums[i]=sc.nextInt();
  18. }
  19.  
  20. int res = sumOfPairEqualsK(k,nums);
  21. System.out.println(res);
  22.  
  23. }
  24.  
  25. public static int sumOfPairEqualsK(int k,int []nums){
  26. HashMap<Integer,Integer> map = new HashMap<>();
  27. int cnt=0;
  28. for(int i=0; i<nums.length; i++){
  29. if(map.containsKey(k - nums[i])){
  30. cnt++;
  31. }
  32. map.put(nums[i],i);
  33. }
  34.  
  35. return cnt;
  36.  
  37. }
  38. }
Success #stdin #stdout 0.15s 56248KB
stdin
5
4
3 2 1 2 5
stdout
2