click here for answer
do a heap sort - in-place algorithm. sorts with O(1) extra space requirements.
traverse through the array finding if the next element is same as the previous element since now the duplicates will be consequetively present.
this is one of the possible answers. if you have a better solution, please post so..
Calculate n*n+1/2 and subtract it from the sum of array.
ReplyDeletesudha,
ReplyDeletecool. your method uses O(n)in all cases with no extra memory. nice answer.
1,2,4,1,5 - sum 13
ReplyDelete5 * 6/2 = 15
13 - 15 = -2
are u missing -2
wrong solution..
@karthik mm. either you missed a 3 in that array or i comprehended the question wrong. I assumed all the numbers being there
ReplyDeleteIf what you had said is the case then sort + fetch as suja suggested would give the best result.
yeah...assigning it back to questioner to provide clear, unambigous questions..:)
ReplyDeletekarthik, sudha,
ReplyDeleteto clarify... the question clearly says one integer is present twice. it does not state anything about finding missing integers.
yikes...ok,right question
ReplyDelete