How can I find a number which occurs an odd number of times in a SORTED array in O(n) time?

后端 未结 15 2018
梦如初夏
梦如初夏 2021-01-30 10:31

I have a question and I tried to think over it again and again... but got nothing so posting the question here. Maybe I could get some view-point of others, to try and make it w

15条回答
  •  伪装坚强ぢ
    2021-01-30 11:19

    Take the middle element e. Use binary search to find the first and last occurrence. O(log(n)) If it is odd return e. Otherwise, recurse onto the side that has an odd number of elements [....]eeee[....]

    Runtime will be log(n) + log(n/2) + log(n/4).... = O(log(n)^2).

提交回复
热议问题