Can you give the answer

debleena_doll2002's Avatar
Ambitious contributor
Given an array of size n. It contains numbers in the range 1 to n. Each number is present at least once except for 2 numbers. Find the missing numbers.

It should be efficient way. means If you can then do it in O(n)
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Try doing it on paper first. What numbers are missing from { 5 2 8 1 10 3 7 4 }?
How did you work it out? OK, if you looked for 1, then 2, then 3 etc, that's inefficient. Work it out again starting at the first element in the array, looking at each element only once.

Hint: You can take notes. For example, how might you keep track of the fact that you've seen a 5 after processing the first element?