Skip to the code
cf.
+50 XP

#372 Largest XOR of Two Session IDs

hard

A load-testing tool hashes pairs of session ids with XOR (^) and wants the pair that differs the most. a ^ b sets a bit to 1 wherever a and b have different bits.

Write largestXor(ids) that returns the largest value of ids[i] ^ ids[j] over all positions i and j. If there is only one id, return 0. Aim for better than checking every pair.

Example 1

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

5 ^ 25 is 00101 ^ 11001 = 11100, which is 28.

Example 2

Input: ids = [7]
Output: 0

With one id, the only pair is the id with itself.

Constraints

  • 1 <= ids.length <= 2 * 10^4
  • 0 <= ids[i] < 2^31
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).