A sharding tool picks two user IDs whose bit patterns differ as much as possible, measured by their XOR. ids holds non-negative 31-bit IDs.
Write maxPairXor(ids) that returns the largest value of ids[i] ^ ids[j] over all pairs i < j. If there is only one ID, return 0.
Checking every pair is O(n²); aim for about 31 × n steps.
Input: ids = [3,10,5,25,2,8] Output: 28
5 ^ 25 = 00101 ^ 11001 = 11100 = 28, the largest of all pairs.
Input: ids = [7] Output: 0
There is no pair, so the answer is 0.
1 <= ids.length <= 1.5 × 10^50 <= ids[i] <= 2^31 - 1Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #674 Most Different Pair of IDs. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.