Find a number where it appears exactly N/2 times

后端 未结 20 1839
旧巷少年郎
旧巷少年郎 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:58

    You can't do this in sublinear time because you need to read the array. To process an array of a million records in logarithmic time would require only reading ~20 (log2) elements--clearly impossible. After all if you assume the first duplicate found is repeated N/2 times it's still O(n) because you may need to look at 500,001 elements to find a duplicate.

    You can do this in O(n) if you assume the integers are nonnegative. It goes like this (pseudo-Java):

    int repeatedNumber = -1; // sentinel value
    int count = 0;
    BitSet bits = new BigSet(); // this bitset needs to have 2^31 bits, roughly 2.1 billion
    boolean duplicate = false;
    for (int i : elements) {
      if (bits[i].isSet()) {
        if (repeatedNumber == -1) {
          repeatedNumber = i;
          count = 1;
        } else if (i == repeatedNumber) {
          count++;
        } else {
          System.out.println("Array has more than one repeated element");
          duplicate = true;
          break;
        }
      } else {
        bits[i].set();
      }
    }
    if (!duplicate && repeatedNumber != -1 && count == elements.length/2) {
      System.out.println(repeatedNumber + " occurred " + count + " times. The rest of the elements are unique");
    } else {
      System.out.println("Not true");
    }
    

    A similar method is used to sort an array of unique integers in O(n) (radix sort).

    0 讨论(0)
  • 2021-01-29 23:58

    If I'm understanding the problem correctly: all we know about the array is it's length and it has (N/2)+1 unique elements, where 1 element is repeated N/2 times(in no specific order).

    I think this suffers a hard limit of O(N) for the solution as you can't really assert (for a generic array) that you've found the number without finding at least 2 of the same number. I dont think there exists a search for an unordered array that can detect a duplicate in O(logN) (please correct me if i'm wrong). You will always need to read at least N/2 +1 elements in the worst case.

    0 讨论(0)
提交回复
热议问题