3 ms·
Add all the numbers in the list up, and then subtract that value from the sum of 1 to 250. That value is the missing number, in O(n) time and O(1) space. This o
by oh_sigh 13y ago
Add all the numbers in the list up, and then subtract that value from the sum of 1 to 250. That value is the missing number, in O(n) time and O(1) space. This only works if all the numbers are only written once though.
- yelnatz 13y agoCan't just do that right away since you have to parse the string first: ...11123... Is that a 1, an 11, a 12, a 112, a 123?
- jffry 13y agoSum all the individual digits of 1-250 (e.g. 11 is 1+1 is 2). Sum all the individual digits in your string. Now you at least know the sum of the missing digits, and how many missing digits there are.