GokulDeep

Check sum pair

Jun 20th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. /* package codechef; // don't place package name! */
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. public class Main
  8. {
  9.     public static void main (String[] args) throws java.lang.Exception,IOException {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         String[] firstLine = br.readLine().split(" ");
  13.         int N = Integer.parseInt(firstLine[0]);
  14.         int K = Integer.parseInt(firstLine[1]);
  15.  
  16.         String[] numsStr = br.readLine().split(" ");
  17.         int[] nums = new int[N];
  18.         for (int i = 0; i < N; i++) {
  19.             nums[i] = Integer.parseInt(numsStr[i]);
  20.         }
  21.  
  22.         getC(K, nums);
  23.     }
  24.  
  25.     private static void getC(int K, int[] nums) {
  26.         Map<Integer, Integer> map = new HashMap<>();
  27.        
  28.  
  29.         int count = 0;
  30.  
  31.         for (int num : nums) {
  32.             int complement = K - num;
  33.  
  34.             if (map.containsKey(complement)) {
  35.                 count += map.get(complement);
  36.             }
  37.  
  38.             map.put(num, map.getOrDefault(num, 0) + 1);
  39.         }
  40.         System.out.println(count);
  41.     }
  42. }
  43.  
  44. //test case 10 failed.
Advertisement
Add Comment
Please, Sign In to add comment