Ctrl K

XOR of a Close Pair

Easy
You are given an array of positive integers values. Call two numbers x and y *close* when |x − y| ≤ min(x, y), meaning the larger one is at most twice the smaller. Pick two close numbers from the array so that their bitwise XOR is as large as possible, and return that value. The same element may be used twice.
Example 1
values=[3, 5, 6, 12] → 10

The close pairs are (3, 5), (3, 6), (5, 6), (6, 12) and every number with itself. The largest XOR is 6 XOR 12 = 10.

Example 2
values=[4, 20] → 0

20 − 4 = 16 is more than 4, so 4 and 20 are not close. Only pairs of a number with itself remain, and their XOR is 0.

Example 3
values=[7, 8, 9, 15] → 15
Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLargest XOR of a Close Pair HardMost Different Pair MediumWords That Fit Each Puzzle HardBest XOR Up the Chain of Command HardRepeat Within Reach Easy
loading Python…
Input
values=[3, 5, 6, 12]
Expected
10

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.