Two elements in array whose xor is maximum

后端 未结 6 1550
野趣味
野趣味 2020-12-22 16:35

Given an array of integers ,You have to find two elements whose XOR is maximum.

There is naive approach --just by picking each element and xoring with other elemen

6条回答
  •  误落风尘
    2020-12-22 17:01

    Ignoring the sign bit, one of the values must be one of the values with the highest significant bit set. Unless all the values have that bit set, in which case you go to the next highest significant bit that isn't set in all the values. So you could pare down the possibilities for the 1st value by looking at the HSB. For example, if the possibilities are

    0x100000
    0x100ABC
    0x001ABC
    0x000ABC
    

    The 1st value of the max pair must be either 0x100000 or 0x10ABCD.

    @internal Server Error I don't think smallest is necessarily correct. I don't have a great idea for paring down the 2nd value. Just any value that isn't in the list of possible 1st values. In my example, 0x001ABC or 0x000ABC.

提交回复
热议问题