r/dailyprogrammer_ideas • u/TheMsDosNerd • Oct 09 '19
[easy] Two sum
Description.
Given are a list of numbers (sorted from lowest to highest) and a target number. Find the two numbers from the list which, when added together, are as close to the target as possible.
Input.
An array of numbers and a target. The array is sorted from smallest to largest. Examples:
[1.1, 2.2, 3.3, 5.5], 4.3
[2, 3, 7, 11, 15], 23Output.
The pair of numbers from that array that comes closest to the target when the two numbers in the pair are summed. Examples:
1.1, 3.3
7, 15
6
Upvotes
View all comments
Show parent comment
1
u/ka-splam Nov 11 '19
Can this be done in O(n) time?