Search code examples
pythonbinary-searchxor

Is there an efficient way to find minimum xor value between a key and elements of a list?


Assume that I have a list of 10 thousand random numbers chosen in this way:

random_num = list(np.random.choice(2**16, 10000, replace=False))

Now, I have a key in the same range (e.g., 1024), and I need to find 10 numbers from the sorted list that have minimum value with the key based on the xor distance:

xor_dist = key ^ random_num[i]

Any suggestion?

I tried to sort the random_num list and then find the xor distance. Because of the xor nature, it doesn't work for all keys.

Also, I was thinking to use banary search to find the key in the list and then check with the neighbors, but again, it doesn't make the correct result for all keys and depends on where the key is in the range.


Solution

  • Sorting is overkill, because it sorts all the numbers in O(n lg n) time, but you don't care about the ordering of 9,990 of the elements.

    Instead, use the heapq module to build a heap (in O(n) time) from which you can extract the 10 smallest in O(lg n) time.

    import numpy as np
    import heapq
    
    random_num = np.random.choice(2**16, 10000, replace=False)
    q = [(x ^ 1024, x) for x in random_num]
    heapq.heapify(q)
    result = [v for _, v in heapq.nsmallest(10, q)]