#C10354. Count Pairs with Given Sum
Count Pairs with Given Sum
Count Pairs with Given Sum
You are given an array of integers and a target sum. Your task is to count the number of pairs of distinct indices (i, j) such that the sum of the elements at these indices equals the target.
More formally, given an array nums and an integer target, count the number of pairs (i, j) with i < j for which:
\(nums[i] + nums[j] = target\)
The array may contain duplicate elements, and each valid pair should be counted only once.
inputFormat
The first line of input contains two integers \(n\) and \(target\) where \(n\) (\(0 \leq n \leq 10^5\)) is the number of elements in the array, and \(target\) is the sum to be formed.
The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single integer which is the number of pairs of indices (i, j) such that \(nums[i] + nums[j] = target\).
## sample5 6
1 5 7 -1 5
3