A shop's "buy two" promotion only applies when the two items together cost strictly less than budget. Marketing wants to know how many different pairs of items qualify.
Write countPairs(prices, budget) that returns how many index pairs i < j have prices[i] + prices[j] < budget. prices is not sorted, and two items with the same price are still different items.
Input: prices = [4,1,3,2], budget = 5 Output: 2
The pairs 1 + 3 and 1 + 2 are under 5. 1 + 4 and 3 + 2 equal 5, which is not strictly less.
Input: prices = [10,20,30], budget = 25 Output: 0
Even the two cheapest items, 10 + 20, cost 30.
0 <= prices.length <= 100000-10^4 <= prices[i], budget <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #101 Count Pairs Under a Budget. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.