PythiloMedium · 20 min · Task 11 of 20 · progress kept in this browser

Count pairs below a limit

Count how many pairs of positions in a list have values adding up to strictly less than a limit.

count_pairs_below([1, 2, 3, 4], 6)  # 4

The pairs are 1+2, 1+3, 1+4 and 2+3.

Your task

Write count_pairs_below(numbers, limit) returning the number of index pairs i < j with numbers[i] + numbers[j] < limit, in O(n log n) time and without changing the input.