Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading