We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You move the left index if the total cost is less than your budget and the right index if it is too much.
[0,2,3,4,5,6] Initially pointing at 0 and 6
With a target of 7, we note that 0+6=6, which is less than 7, so we move the left pointer to 2. 2+6=8 is more than 7, so we move the right pointer and get 2+5=7
With a target of 4, we see that 0+6=6, which is more than 4 so we move the right pointer. 0+5=5, which is also more than 4 so we move the right pointer again and discover the answer.
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
You move the left index if the total cost is less than your budget and the right index if it is too much.
[0,2,3,4,5,6] Initially pointing at 0 and 6
With a target of 7, we note that 0+6=6, which is less than 7, so we move the left pointer to 2. 2+6=8 is more than 7, so we move the right pointer and get 2+5=7
With a target of 4, we see that 0+6=6, which is more than 4 so we move the right pointer. 0+5=5, which is also more than 4 so we move the right pointer again and discover the answer.