A coastal safety authority operates a row of n radio beacons, each broadcasting at a fixed signal strength. Whenever two beacons transmit inside the same corridor at once, regulations require their combined broadcast strength to stay strictly below a compliance ceiling, or the pair must be reported for retuning. Given every beacon's strength and the ceiling value, count how many unordered pairs of distinct beacons already satisfy the rule on their own — that is, have a combined strength strictly less than the ceiling.
n and ceiling.n integers strength_1 … strength_n, the broadcast strength of each beacon.Print a single integer: the number of index pairs (i, j) with 0 <= i < j <= n-1 such that strength_i + strength_j < ceiling.
1 <= n <= 4000-10000 <= strength_i <= 10000-20000 <= ceiling <= 20000Example 1
Input
4 6 1 2 3 4
Expected
4
Explanation
Strengths are [1,2,3,4]. The pairs with sum below the ceiling of 6 are (1,2)=3, (1,3)=4, (1,4)=5, and (2,3)=5 — four pairs in total; (2,4)=6 and (3,4)=7 do not qualify since they are not strictly less than 6.
Example 2
Input
3 0 -2 -1 3
Expected
1
Explanation
The three possible pairs sum to (-2)+(-1)=-3, (-2)+3=1, and (-1)+3=2. Only -3 is strictly less than the ceiling of 0, so the answer is 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →