Ctrl K

Cheapest XOR Pairing

Hard
You get two arrays, left and right, both of length n. Split their elements into n pairs so that every pair takes exactly one element from left and exactly one from right. A pair costs the bitwise XOR of its two numbers, and the cost of the whole split is the sum over all pairs. Return the smallest possible cost.
Example 1
left=[5, 8], right=[9, 4] → 2

Pairing 5↔4 and 8↔9 costs 1 + 1 = 2; pairing 5↔9 and 8↔4 would cost 12 + 12 = 24.

Example 2
left=[7, 0, 3], right=[3, 6, 1] → 2

The best choice is 7↔6, 0↔1, 3↔3, for a total of 1 + 1 + 0 = 2.

Example 3
left=[12], right=[12] → 0
Constraints
  • 1 ≤ n ≤ 14
  • len(left) = len(right) = n
  • 0 ≤ left[i], right[i] ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardAssembling Fragments HardSquare-Sum Chains HardSmallest Team With Every Skill Hard
loading Python…
Input
left=[5, 8], right=[9, 4]
Expected
2

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