#K41672. Count Distinct Pairs with Given Sum

    ID: 26918 Type: Default 1000ms 256MiB

Count Distinct Pairs with Given Sum

Count Distinct Pairs with Given Sum

You are given an array of integers and an integer k. Your task is to find the number of distinct pairs (i, j) (with i ≠ j) such that the sum of the two numbers is exactly k. A pair is considered distinct based solely on the values, not on the indices. In other words, if a pair of numbers (a, b) sums up to k, it should be counted only once even if they occur multiple times in the array.

Note: The pair (a, b) is considered the same as the pair (b, a). Also, when both numbers in the pair are the same, there must be at least two occurrences in the array for the pair to be valid.

Example: For array [1, 5, 3, 3, 3, 1] and k = 6, the valid distinct pairs are (1, 5) and (3, 3), so the answer is 2.

inputFormat

The input is given via stdin and has the following format:

  1. The first line contains two integers n and k, where n is the number of elements in the array and k is the target sum.
  2. The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer representing the number of distinct pairs whose elements sum up to k.

The output must be written to stdout.

## sample
6 6
1 5 3 3 3 1
2