Duplicate values forming pairs
Input
count_pairs_with_sum([1, 2, 3, 2], 4)
Output
2
Pairs: 1+3 and 2+2 (the two different 2s form one pair).
Full lesson preview
Count unordered pairs with a target sum using itertools.combinations. Practice enumerating unique index pairs.
Problem statement
Task
Examples
Input
count_pairs_with_sum([1, 2, 3, 2], 4)
Output
2
Pairs: 1+3 and 2+2 (the two different 2s form one pair).
Input format
Output format
Constraints
Samples
Input
count_pairs_with_sum([2, 2, 2], 4)
Output
3
Three unordered pairs can be formed from three 2s: choose any two of them.