856. Lowest XOR Pairing
Two sensor arrays a and b of the same length n are recorded in a lab. To compare them, you may reorder b in any way you like, leaving a untouched. After reordering, the mismatch score is the sum over all positions i of a[i] XOR b[i], where XOR is the bitwise exclusive-or.
Return the smallest mismatch score reachable over all reorderings of b. Equal values may appear several times in either array. For n = 1 the answer is just a[0] XOR b[0].
Example 1
- Input:
- a = [6,10,3], b = [5,9,12]
- Output:
- 19
- Explanation:
Pairing a-values [6, 10, 3] with b-values [5, 12, 9] gives xors [3, 6, 10] that add up to 19, the smallest over all 6 pairings.
Example 2
- Input:
- a = [1,2], b = [2,1]
- Output:
- 0
- Explanation:
Pairing 1 with 1 and 2 with 2 gives 0 xor 0 = 0, which cannot be beaten.
Constraints
- 1 ≤ n ≤ 14, a.length = b.length = n
- 0 ≤ a[i], b[i] ≤ 108
- The answer fits in a 32-bit signed integer
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(2^n * n)
- Space
- O(2^n)