#K71812. Count Pairs with Target Sum
Count Pairs with Target Sum
Count Pairs with Target Sum
Given an array \( a \) of \( n \) integers and an integer \( T \), your task is to count the number of distinct pairs of indices \( (i, j) \) (with \( i < j \)) such that \( a_i + a_j = T \).
The problem requires you to efficiently calculate the number of pairs that add up to the target value. For example, consider the array [1, 5, 7, -1] with target \( T = 6 \). The valid pairs are (1, 5) and (7, -1), so the answer is 2.
Input Specification: The input is given in multiple lines. The first line contains an integer \( n \) representing the number of elements. The second line contains \( n \) space separated integers representing the array. The third line contains the integer \( T \), the target sum.
Output Specification: Output a single integer representing the number of distinct pairs of indices \( (i, j) \) such that \( a_i + a_j = T \).
inputFormat
The input is read from standard input and it has the following format:
n a1 a2 a3 ... an T
Where:
n
is the number of elements in the array.a1, a2, ..., an
are the integers in the array.T
is the target sum.
outputFormat
Output a single integer to standard output indicating the number of distinct pairs \( (i, j) \) such that their sum equals \( T \) and \( i < j \).
## sample4
1 5 7 -1
6
2