Find a number where it appears exactly N/2 times

后端 未结 20 1893
旧巷少年郎
旧巷少年郎 2021-01-29 23:17

Here is one of my interview question. Given an array of N elements and where an element appears exactly N/2 times and the rest N/2 elements are unique

20条回答
  •  佛祖请我去吃肉
    2021-01-29 23:40

    Contrary to answers above, there is a solution with worst case behavior as requested, O(log n) RUN TIME. The problem is not to find a solution with O(log N) comparisons worst case (which is impossible), but to do it O(log N) time.

    If you can do N comparisons in parallel, the solution is a trivial divide-and-conquer. Not very practical in the real world, but it's an interview question, not a real-world problem.

    Update: I think you can do it in constant time with O(N) processors

提交回复
热议问题