#C9415. Count Pairs with Given Sum
Count Pairs with Given Sum
Count Pairs with Given Sum
Given an array of integers and a target sum, your task is to count the number of pairs of elements in the array whose sum is equal to the target value. Each pair is counted only once based on the order as they appear in the array (i.e. for every element, count all previous occurrences of its complement).
The problem can be formalized as follows: Given an array \(A = [a_1, a_2, \dots, a_n]\) and an integer \(T\), compute the number of pairs \((i, j)\) with \(i < j\) such that \(a_i + a_j = T\).
Note: The input will be provided in two lines. The first line contains the array elements separated by spaces, and the second line contains the target sum.
inputFormat
The input consists of two lines:
- The first line contains a sequence of integers separated by spaces, representing the array \(A\).
- The second line contains a single integer, representing the target sum \(T\).
outputFormat
Output a single integer representing the number of pairs whose sum equals the target \(T\>.
## sample1 5 7 -1 5
6
3