Loading
Loading
A lock maker pairs two key codes to form a combined code, computed as their bitwise XOR. The more bits the combination sets high, the harder it is to guess, so the designers want the strongest pairing available.
Given non-negative integers codes, return the largest value of codes[i] ^ codes[j] over all pairs with i ≤ j. (Pairing a code with itself gives 0, so the answer is at least 0.)
codes = [3, 10, 5, 25, 2, 8]bit 4: best = 10000some prefix pair differs at bit 4 (e.g. 25 vs 5)bit 3: best = 11000prefixes 11 and 00 exist (25 and 5)bit 2: best = 11100110 and 001 exist (25 and 5)bit 1: best = 11100no pair of prefixes XORs to 11110bit 0: best = 11100no pair XORs to 11101 → answer 2828Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n · B) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
5 XOR 25 = 00101 XOR 11001 = 11100 = 28.
Example 2
A code XORed with an identical code gives 0.