Possible Duplicate:
Easy interview question got harder: given numbers 1..100, find the missing number(s)
Find the missing and duplicate elements in an array in linear time and constant space
I saw an interesting Question on one forum.
you have 100 elements from 1 to 100 but byy mistake one of those number overlapped another by repeating itself.
E.g. 1,99,3,…,99,100
Array is not in sorted format , how to find the repeating number ?
I know Hash can do it O(n) time and O(n) space, I need O(1) space.
We can do it in O(n) and constant space:
index = Math.abs(a[i]) - 1indexindex+1) as answer, as it means we have seen this index before.“”