Cable Car Pairs
Medium · hash map
A mountain cable car has small two-person cabins. For the cabin to balance, the two hikers who ride together must weigh exactly limit kilograms between them, no more and no less.
The list weights holds the weight of each hiker waiting in the queue (whole kilograms). Different hikers can have the same weight.
Count the pairs of different hikers whose weights add up to exactly limit. A pair of hikers is the same pair whichever of them you name first, so count it once.
Examples
Example 1
Input: weights = [60, 80, 70, 70, 50, 90], limit = 140
Output: 3
The pairs are (60, 80), (70, 70) and (50, 90): hikers 0 and 1, hikers 2 and 3, and hikers 4 and 5.
Example 2
Input: weights = [5, 5, 5, 5], limit = 10
Output: 6
Every pair of the four equal hikers works, and there are six pairs.
Example 3
Input: weights = [10, 20, 30], limit = 100
Output: 0
Constraints
- 1 <= len(weights) <= 10^4
- 1 <= weights[i] <= 10^6
- 2 <= limit <= 2 * 10^6