You have given an array from 1 to N and numbers also from 1 to N. But more than one number is missing and some numbers have repeated more than once. Find the algo with running time O(n).
Combinea all msb of each integer as bit vector, traverse the array, and set corresponding msb, if you find the msb is set, you get duplicate number. After traversal, scan all msbs again, if the msb is unset, the number is missing
Missing one value or duplicate one value: (n+1)*n/2
Missing one value and duplicate one values:
(n+1)*n/2
n(n+1)(2n+1)/6
m-d=
(m+d)(m-d)=
…
Missing two values or duplicate two values
(n+1)*n/2
n(n+1)(2n+1)/6
m1+m2
m1*m2
…
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment