retagged by
926 views
2 votes
2 votes

as we already know the range,so can we use counting sort??and complexity of counting sort is O(n+k)

what other method is there to know the missing number??

retagged by

2 Answers

Best answer
5 votes
5 votes
  • $XOR$ all the array elements like a[0] XOR a[1] XOR a[2] XOR a[3].............. XOR a[n].

Say this as A.

  • $XOR$ all the natural numbers like 0 XOR 1 XOR 2 XOR 3 XOR ................... XOR N.

Say this as B.

The missing number will be A XOR B.


  • Another method is to use the sum of $1$st $N$ natural numbers and substract all the elements from the sum .
selected by
1 votes
1 votes
We know that integers are between 0 to n.

 

Create an auxilary array B of size (n+1) with index 0 to n

Now, go on scanning the integers one by one and then place the integer into its original place in array B. If the number doesnt map to any place, you got the unknown integer.

So, O(n)

Related questions

8 votes
8 votes
2 answers
1
2 votes
2 votes
1 answer
2
yes asked Oct 6, 2015
1,385 views
for example array contain a[1 2 3 3 3 3 3 4 5] then retun(1)
0 votes
0 votes
1 answer
3
0 votes
0 votes
3 answers
4