Knowing the XOR of odd Number

邮差的信 提交于 2020-01-06 09:05:41

问题


Given an array of odd numbers or even numbers, how can one efficiently get pairs of this array whose XOR == 2?

For example:

arr = [4,10,2,6,8]
pairs are: [(4,6), (8,10)] #4^6 == 2, 8^10 == 2

Or:

arr = [5,9,3,7,11]
pairs are: [(9,11), (5,7),]

I did this to get them (brute-force)

for i in combinations(inev,2):#inev is the list of indices (positions of the numbers in the array)

        if not (arr[i[0]] ^ arr[i[1]]) & 1 and (arr[i[0]] ^ arr[i[1]]) == 2:

                print arr[i[0]], arr[i[1]] #I print the pair 

来源:https://stackoverflow.com/questions/52239393/knowing-the-xor-of-odd-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!