Skip to content
Decoder

Maximum XOR of Two Numbers in an Array

Medium

Problem

Given an array of non-negative integers nums, return the largest value of nums[i] ^ nums[j] over all pairs with 0 <= i <= j < n (so a number may be paired with itself, giving 0).

Example 1

Input
nums = [3, 10, 5, 25, 2, 8]
Output
28
Why
5 ^ 25

Example 2

Input
nums = [8, 10, 2]
Output
10
Why
8 ^ 2

Example 3

Input
nums = [7]
Output
0

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • 0 <= nums[i] <= 2^31 - 1

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.