Skip to content
BytePatterns

Pairs Below a Budget

EasySorting#sorting#two-pointers~20m

Problem

A shop lists item prices, and a price can even be negative after a refund. Count the pairs of two different items whose prices add up to strictly less than a budget. Two items at different positions form a separate pair even if their prices are equal.

Examples

Input:  prices = [4, -2, 0, 3, 1], budget = 3
Output: 5
Why:    (4, -2), (-2, 0), (-2, 3), (-2, 1) and (0, 1) all add up to less than 3
Input:  prices = [2, 2, 2], budget = 5
Output: 3
Why:    each of the three ways to pick two of the 2s adds up to 4
Input:  prices = [5], budget = 100
Output: 0
Why:    edge case, one item cannot form a pair

Hints

0 / 3

Stuck on the idea rather than the code? O(n²) and Nested Loops covers it.