#K80912. Count Pairs with Sum
Count Pairs with Sum
Count Pairs with Sum
You are given an array of integers and a target integer \(T\). Your task is to determine the number of pairs \( (i, j)\) with \(i < j\) such that the sum of the elements at positions \(i\) and \(j\) equals \(T\). In mathematical terms, find the number of pairs satisfying \(a_i + a_j = T\). Note that when identical numbers appear multiple times, each distinct pair is counted. For instance, if the array is [1, 2, 3, 4] and \(T=5\), there are 2 valid pairs: (1,4) and (2,3).
inputFormat
The input is read from stdin
and consists of two lines. The first line contains two integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum. The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
Output a single integer to stdout
which is the count of all pairs \( (i, j)\) such that \(a_i + a_j = T\) and \(i < j\).
3 7
1 2 3
0
</p>