r/dailyprogrammer • u/oskar_s • Jun 20 '12
[6/20/2012] Challenge #67 [intermediate]
You are given a list of 999,998 integers, which include all the integers between 1 and 1,000,000 (inclusive on both ends) in some unknown order, with the exception of two numbers which have been removed. By making only one pass through the data and using only a constant amount of memory (i.e. O(1) memory usage), can you figure out what two numbers have been excluded?
Note that since you are only allowed one pass through the data, you are not allowed to sort the list!
EDIT: clarified problem
- Thanks to Cosmologicon for suggesting this problem at /r/dailyprogrammer_ideas? Do you have a problem you think would be good for us? Why not head over there and suggest it?
11
Upvotes
View all comments
Show parent comment
1
u/ashashwat Jun 20 '12
Rest all logic remaining same, to calculate sum of numbers of list as well as square of numbers of list writing two different functions make the code looks cleaner. While if we try to do that in one pass, it makes the code ugly. I am strictly talking about functional programming. In a language like C, using loops and calculating sum and square in the same loop is an obvious choice thereby making it in one-pass.
Example in haskell here,