Jason4L's blog

By Jason4L, history, 3 months ago, In English

Did you know that the minimum XOR pair of an array can be found by sorting the array, then checking the XOR of adjacent elements.

Can anyone prove why?

  • Vote: I like it
  • +17
  • Vote: I do not like it

»
3 months ago, # |
  Vote: I like it -21 Vote: I do not like it

That is because the difference in the set bits in the adjacent elements in the sorted array is the minimum. For Example: elements may be 1000, 1001 or 1010 in binary form.

If you consider adjacent elements like 0111 and 1000(again binary form), obviously they won't contribute anything to the minimum, but most likely the element next to 1000 ahead of it in the sorted array will differ in less number of set bits, which may contribute to the minimum. The same analogy goes for elements behind 0111 in the sorted array.

In this way, we can get the minimum xor of any pair, which would obviously have least difference in number of set bits.

»
3 months ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

Consider a greedy algorithm on a 0-1 Trie.

»
3 months ago, # |
  Vote: I like it +3 Vote: I do not like it
»
3 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Let's look at all possible pairs of elements in the array. The minimum XOR is achieved in one of those pairs where the length of the common prefix in the bit representation is the longest. This is because it corresponds to the maximum prefix of zeros at the beginning of the XOR. Hence, the minimum XOR is achieved in one of such pairs. Now, notice that for any such pair, it is true that its elements are consecutive in the sorted array because the elements have the form $$$prefix0 \dots$$$ and $$$prefix1 \dots$$$ If there were at least one more element $$$a_i$$$ between them, the common prefix of $$$a_i$$$ with one of the elements of the pair would be greater than the prefix of the pair. $$$(a_i = prefix0\dots\ or\ prefix1\dots)$$$ But we initially considered pairs with the longest matching bit prefix. Contradiction!

»
3 months ago, # |
  Vote: I like it +3 Vote: I do not like it

does anyone has list of problems like this where you can learn new XOR properties , such as state above.

»
3 months ago, # |
  Vote: I like it +3 Vote: I do not like it

https://atcoder.jp/contests/abc308/tasks/abc308_g

you can go through the editorial of this problem

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I've understood it from the above comments now. Cheers

»
3 months ago, # |
  Vote: I like it +4 Vote: I do not like it

Similar approach can be applied to find a maximal AND pair.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +44 Vote: I do not like it

    Does not work for maximal AND pair. Consider 1 4 9 Here (1 & 4) = 0, (4 & 9) = 0 but maximal AND (1 & 9) = 1

»
6 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Given some set of non-negative integers $$$S$$$, we want to show the following:

$$$a \oplus b = \min_{x, y \in S} x \oplus y \implies \not \exists c \in S, \text{such that } a < c < b$$$

We strive for a contradiction. Suppose there does exist such a $c \in S$. Let $$$i$$$ be the first (leftmost) index such that $$$c_i = 0$$$ and $$$b_i = 1$$$, where $$$x_i$$$ represents the $$$i$$$-the digit from the left in the binary representation of $$$x$$$. Now if $$$a_i = 0$$$, then $$$a \oplus c < a \oplus b$$$. If $$$a_i = 1$$$, then there must exist some $$$j < i$$$ such that $$$b_j = 1$$$ and $$$a_j = 0$$$ (since $$$a < c < b$$$). Here, $$$a_j \oplus b_j = 1$$$ but $$$b_j = c_j \implies b_j \oplus c_j = 0$$$. Thus $$$b \oplus c < a \oplus b$$$. In both cases, we have arrived at a contradiction. $$$\square$$$

Thinking about your question made me realize a very generally applicable idea: if you can prove some property about the desired condition, and that property is only satisfied by finitely many things, then you can possibly iterate over all those things to find something that satisfies the desired condition.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    yeah, this is the most natural proof for me. Proving it for an array of three elements is both necessary and sufficient.