A depot has n parcels, each with an integer weight. A delivery cart can carry exactly two parcels at once, but only if their combined weight does not exceed a limit L. Count how many unordered pairs of distinct parcels (i, j) with i < j satisfy weight[i] + weight[j] <= L. The parcels are not given in any particular order.
Line 1: an integer n.
Line 2: n space-separated integers, the parcel weights.
Line 3: an integer L, the combined-weight limit.
A single integer: the number of unordered pairs whose weights sum to at most L.
Example 1
Input
4 1 2 3 4 5
Expected
4
Explanation
Pairs with sum at most 5: (1,2)=3, (1,3)=4, (1,4)=5, (2,3)=5. That is 4 pairs; (2,4)=6 and (3,4)=7 are too heavy.
Example 2
Input
3 5 6 7 4
Expected
0
Explanation
The lightest pair is 5+6=11, already above 4, so no pair qualifies.
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 →