import java.util.*;
public class Main {
static int countPairsWithSum(int[] b, int k) {
int count = 0;
Map
<Integer, Integer
> seen
= new HashMap
<>();
for (int j = 0; j < b.length; ++j) {
int complement = k - b[j];
if (seen.containsKey(complement)) {
count++;
}
seen.put(b[j], j);
}
return count;
}
public static void main
(String[] args
) { int[] b = {1, 2, 3, 4, 5};
int k = 6;
int count = countPairsWithSum(b, k);
System.
out.
println("Count of pairs with sum " + k
+ " is: " + count
); }
}
aW1wb3J0IGphdmEudXRpbC4qOwogCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHN0YXRpYyBpbnQgY291bnRQYWlyc1dpdGhTdW0oaW50W10gYiwgaW50IGspIHsKICAgICAgICBpbnQgY291bnQgPSAwOwogICAgICAgIE1hcDxJbnRlZ2VyLCBJbnRlZ2VyPiBzZWVuID0gbmV3IEhhc2hNYXA8PigpOwogCiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBiLmxlbmd0aDsgKytqKSB7CiAgICAgICAgICAgIGludCBjb21wbGVtZW50ID0gayAtIGJbal07CiAgICAgICAgICAgIGlmIChzZWVuLmNvbnRhaW5zS2V5KGNvbXBsZW1lbnQpKSB7CiAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHNlZW4ucHV0KGJbal0sIGopOwogICAgICAgIH0KIAogICAgICAgIHJldHVybiBjb3VudDsKICAgIH0KIAogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIGludFtdIGIgPSB7MSwgMiwgMywgNCwgNX07CiAgICAgICAgaW50IGsgPSA2OwogICAgICAgIGludCBjb3VudCA9IGNvdW50UGFpcnNXaXRoU3VtKGIsIGspOwogCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJDb3VudCBvZiBwYWlycyB3aXRoIHN1bSAiICsgayArICIgaXM6ICIgKyBjb3VudCk7CiAgICB9Cn0=