Pairs That Add Up
Count the pairs of positions (i, j) with i < j such that nums[i] + nums[j] == target.
Positions are what is counted: equal values at different positions make different pairs. Return the count, which is 0 when nums has fewer than two items.
Example 1
nums [1,5,3,3,7]
target 6
returns 2
1 + 5 and 3 + 3 (the two 3s at positions 2 and 3) add up to 6: two pairs.
Example 2
nums [2,2,2]
target 4
returns 3
Any two of the three 2s add up to 4: three pairs of positions.
Constraints
0 <= len(nums) <= 100-1000 <= nums[i] <= 1000-2000 <= target <= 2000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.