Find a number where it appears exactly N/2 times

后端 未结 20 1868
旧巷少年郎
旧巷少年郎 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条回答
  •  梦毁少年i
    2021-01-29 23:36

    My Answer was,

    1. Divide N elements into [N/3] parts (i.e) each part will have 3 elements.
    2. Now compare these 3 elements among each other. - 3 comparisions
    3. Atleast one of the part will have two copies of the same element. Hence the number.

    Runtime - O(N)

提交回复
热议问题