Count balanced pairs

Write codeMedium14 min

Your task

A review site collects integer scores for a film, one per reviewer, in submission order. The editors call two reviewers a "balanced pair" when their scores add up to exactly target. Given the score list and the target, count the number of unordered reviewer pairs (i, j) with i < j whose scores sum to target. Every index pair counts even when the values are equal. For example, scores [3, 7, 5, 5, 7] with target 10 contain three balanced pairs: the 3 with each 7, and the two 5s together. Aim for a single pass.

Examples

rating_pairs(ratings: list[int], target: int) -> int
Example 1
Inputrating_pairs([3, 7, 5, 5, 7], 10)
Output3