There is an array of size n (numbers are between 0 and n - 3) and only 2 numbers are repeated. Elements are placed randomly in the array.
E.g. in {2, 3, 6, 1, 5, 4
Here is an algorithm that uses order statistics and runs in O(n).
You can solve this by repeatedly calling SELECT with the median as parameter.
You also rely on the fact that After a call to SELECT,
the elements that are less than or equal to the median are moved to the left of the median.
SELECT on A with the median as the parameter.floor(n/2) then the repeated values are right to the median. So you continue with the right half of the array.For example:
A={2, 3, 6, 1, 5, 4, 0, 3, 5} n=9, then the median should be the value 4.SELECTA={3, 2, 0, 1, <3>, 4, 5, 6, 5} The median value is smaller than 4 so we continue with the left half.A={3, 2, 0, 1, 3}SELECTA={1, 0, <2>, 3, 3} then the median should be 2 and it is so we continue with the right half.A={3, 3}, found.This algorithm runs in O(n+n/2+n/4+...)=O(n).