Loading
Loading
A sorted price list is scanned for two items whose prices add up to a budget exactly.
Return the two positions, smaller first. Exactly one such pair exists. Because the list is sorted, no extra memory is needed.
prices = [2, 4, 7, 11, 15], budget = 18left=0 (2), right=4 (15)17 < 18 → raise the floorleft=1 (4), right=4 (15)19 > 18 → lower the ceilingleft=1 (4), right=3 (11)15 < 18 → raise the floorleft=2 (7), right=4? no — left=2, right=3continue until 7 + 11 = 18[2, 4]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
7 + 11 = 18.
Example 2