Блог пользователя -synx-

Автор -synx-, история, 9 лет назад, По-английски

Given a list of n numbers in which all but one repeat exactly k times, but the remaining one appears less than k times (and at least once).
Find this number (which repeats less than k times).
Expected Complexity
Time  ≤ O(nlgk)
Memory  ≤ O(lgk)

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
 
Проголосовать: нравится -16 Проголосовать: не нравится

Use quicksort to sort in nlogk and then traverse array to find element which occurs less than k times in O(n).

»
9 лет назад, скрыть # |
← Rev. 4  
Проголосовать: нравится +4 Проголосовать: не нравится

Hint: for k = 2 we have the classical 'xor all elements' problem. Xorring m-bit numbers is just adding vectors in . Can you generalize this?

EDIT: I am assuming here that the number of bits is considered O(1).

  • »
    »
    9 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, it can be generalized to xor base k, but the issue that does not arise in k=2 case is that number is present exactly twice or exactly once, which greatly simplifies the problem (the answer is just xor of all numbers),
    but for general k, how do we retrieve the number which repeats greater than 1 time and less than k times?

    • »
      »
      »
      9 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      The number repeats 1 ≤ i < k times, so all positions in the final vector will have their value set to 0 (this bit is turned off in the required number) or to i (this bit is turned on in the required number).

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For each bit store the value modulo k. Get the number, divide it by (n%k).