Consider an array of 'n' elements which contains all except 2 numbers
from 1(n + 2). How can we find the 2 missing elements?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
Algorithm Geeks group.
To post to this
use hashing.
On Wed, Jul 29, 2009 at 4:50 PM, Vijayasarathy K vijaykan@gmail.comwrote:
Consider an array of 'n' elements which contains all except 2 numbers
from 1(n + 2). How can we find the 2 missing elements?
--
Ciao,
Ajinkya
ok.. great question
find the sum of the series , and the squares of the elements in the series
Let Sum = S
Sum of squares = P
now find the sum of the of all numbers from 1... (n+2) and their squares,
Let these be
A= (n+2)(n+3)/2 (for n = (n)(n+1)/2)
Here is a pseudocode for one of the solutions
sumN = sum of all the elements of the arraysumN2 = sum of 1 to (n+2)
sumofXY = sumN2 - sumN /*sum of the interested integers, say x, y*/
xorN = XOR of all the elements of the array
xorN2 = XOR of 1 to (n+2)
xorofXY = xorN XOR xorN2 /*XOR of x, y*/
nice one channa.. much like my solution.. same complexity n memory
requiements
but pulling that wonderful use of the Xor gate was great show indeed.. very
impressive solution
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Let the 2 missing numbers be a and b .
Let the sum of the 2 missing numbers be S , product be P .
a+b=S -(1)
ab=P -(2)
Solve (1) and (2)
On Wed, Jul 29, 2009 at 7:30 PM, Vikram Sridar vikramsridar...@gmail.comwrote:
nice one channa.. much like my solution.. same complexity n memory