Suppose that out of the integers from 1 to , you are given of them.
What is the order of complexity of finding these 2 missing numbers?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let the 2 missing numbers be x , y .
O ( n ) . Find the sum of the n − 2 number.
O ( n ) . Find the sum of squares of the n − 2 numbers.
O ( 1 ) .. We know that the sum of the n numbers is 2 n ( n + 1 ) , and the sum of the n squares is 6 n ( n + 1 ) ( 2 n + 1 ) .
O ( 1 ) . We know x + y and x 2 + y 2 by subtracting. We then solve the quadratic to find these 2 numbers.
Hence, we have an O ( n ) algorithm to find the numbers.
Now, since we need to read all of the input, the minimum time is of the order O ( n ) .
Thus, the answer is O ( n ) .