Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package codechef; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- public class Main
- {
- public static void main (String[] args) throws java.lang.Exception,IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String[] firstLine = br.readLine().split(" ");
- int N = Integer.parseInt(firstLine[0]);
- int K = Integer.parseInt(firstLine[1]);
- String[] numsStr = br.readLine().split(" ");
- int[] nums = new int[N];
- for (int i = 0; i < N; i++) {
- nums[i] = Integer.parseInt(numsStr[i]);
- }
- getC(K, nums);
- }
- private static void getC(int K, int[] nums) {
- Map<Integer, Integer> map = new HashMap<>();
- int count = 0;
- for (int num : nums) {
- int complement = K - num;
- if (map.containsKey(complement)) {
- count += map.get(complement);
- }
- map.put(num, map.getOrDefault(num, 0) + 1);
- }
- System.out.println(count);
- }
- }
- //test case 10 failed.
Advertisement
Add Comment
Please, Sign In to add comment