Prefer the opposite bit
transcript
Two lists of numbers. Find the pair with the largest xor. Trying every pair is far too slow for big lists.
Twenty thousand numbers make 400,000,000 pairs. A trie answers all queries in 800,000 steps. Store the old numbers in a trie.
Write each number in twenty bits. Start at the highest bit. Each node has a zero child and a one child. A number is then one path down the trie.
The new number is five: one, zero, one. Prefer the opposite bit at each step. Only two starts with a zero. Five xor two is seven.
A high bit outweighs all lower bits. So decide the high bits first. Preferring the same bit finds the nearest number.
Each number adds at most twenty nodes. Each query takes twenty steps. There is no loop over pairs at all.
Read bits from the highest. Prefer the opposite child. Full breakdown in the lecture.