Skip to the code
cf.
+50 XP

#674 Most Different Pair of IDs

hard

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.

Example 1

Input: ids = [3,10,5,25,2,8]
Output: 28

5 ^ 25 = 00101 ^ 11001 = 11100 = 28, the largest of all pairs.

Example 2

Input: ids = [7]
Output: 0

There is no pair, so the answer is 0.

Constraints

  • 1 <= ids.length <= 2 × 10^4
  • 0 <= ids[i] <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).