assume I have sorted array and I want to count the occurrences of element X.
pseudocode:
SET Lo to 1
SET Hi to array length
WHILE Lo <= Hi
SET Mid to (Lo + Hi) / 2
IF X < array[Mid] THEN
SET Hi to Mid - 1
ELSE IF X > array[Mid] THEN
SET Lo to Mid + 1
ELSE
RETURN Mid
ENDIF
ENDWHILE
RETURN -1
now assume I want to find all the occurrences of all numbers in the array but I didn't succeed.
for example - A=[1,1,2,2,2,2,5,5,5] return (1,2),(2,4),(5,3)
the algorithm has to be O(log(n))
help?