FunctionRacerProblemsRace

Minimum XOR Pairing

HardBitsPython, JavaScript, TypeScript, Java, C++

You are given two lists a and b of the same length n. Pair every item of a with a different item of b, so that each item of b is used exactly once, and add up the XOR (^) of every pair. Return the smallest total that any such pairing can reach.

Example 1

a [1,2]
b [2,3]
returns 2

Pair 1 with 3 and 2 with 2: (1 ^ 3) + (2 ^ 2) = 2 + 0 = 2, better than the other pairing, which gives 3 + 1 = 4.

Example 2

a [1,0,3]
b [5,3,4]
returns 8

Pair 1 with 5, 0 with 4 and 3 with 3: 4 + 4 + 0 = 8, and none of the other five pairings does better.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems