You are given an array of n + 2 elements. All elements of the array are in the range from 1 to n. And all elements occur once, except for two numbers that occur twice
Let's change this value a bit and move on only with n , not n+2 , and the first part of the problem statement will become
You are given an array of n elements. All elements of the array are in the range from 1 to n
So, now you know that you have an array, the numbers in the array start at 1 and go up one for each element of the array. So, if you have 10 elements, the array will contain numbers from 1 to 10. 5 elements, you have from 1 to 5, etc.
It follows that the numbers stored in the array can be used to index the array. that is, you can always say A[A[i]] , where I <= size A. for example. A={5,3,4,1,2}; print A[A[2]]
Now add in one duplicate number. The algorithm takes the value of each number in the array and visits this index. We know that if we visit the same index twice, we know that we found a duplicate.
How do we know if we visit the same index twice?
Yup, we change the sign of the number in each index that we visit, if the sign has already changed, we know that we are already here, ergo, this index (and not the value stored in the index) is a duplicate number.
You can achieve the same result by storing a second array of booleans initialized to false. This algroism is becoming
A={123412} B={false, false, false, false} for(i = 1; i <= 4; i++) { if(B[A[i]]) // Duplicate else B[A[i]] = true; }
However, in the MS question, you change the sign of the element in instead of setting the boolean to B.
Hope this helps,