Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Bit Manipulation

Minimum XOR Sum of Two Arrays

Hard · Bit Manipulation · Asked at Google, Amazon, Codenation

Arrays A and B both have n elements. Rearrange B so that (A[0] ^ B[0]) + (A[1] ^ B[1]) + … + (A[n−1] ^ B[n−1]) is as small as possible (^ is bitwise XOR). Print that minimum sum.

Input

First line: n. Second line: array A. Third line: array B.

Output

The minimum XOR sum.

Constraints

Bitmask DP: dp[mask] = the best sum when the first popcount(mask) elements of A are paired with the elements of B in mask. dp[mask | (1 << j)] = min(…, dp[mask] + (A[popcount(mask)] ^ B[j])). O(n · 2^n).

Example 1

Input

2
1 2
2 3

Output

2

Example 2

Input

3
1 0 3
5 3 4

Output

8

Hints

  1. n ≤ 14 is a strong hint for a bitmask over the elements of B.
  2. Pair A’s elements in order: A[0] first, then A[1], …
  3. If mask tells which B elements are used, popcount(mask) tells which A element comes next.

Approach

dp[mask] = minimum XOR sum after pairing A[0 … popcount(mask) − 1] with the B elements in mask. From each reachable mask, let i = popcount(mask) and try every unused j: dp[mask | 1 << j] = min(…, dp[mask] + (A[i] ^ B[j])). dp[full mask] is the answer. There are 2^14 masks with 14 choices each — about 2.3 × 10^5 transitions.

Complexity: O(n · 2^n) time · O(2^n) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<long long> A(n), B(n);
    for (auto &x : A) cin >> x;
    for (auto &x : B) cin >> x;
    vector<long long> dp(1 << n, LLONG_MAX);
    dp[0] = 0;
    for (int mask = 0; mask < (1 << n); mask++) {
        if (dp[mask] == LLONG_MAX) continue;
        int i = __builtin_popcount(mask);
        if (i == n) continue;
        for (int j = 0; j < n; j++)
            if (!(mask & (1 << j))) dp[mask | (1 << j)] = min(dp[mask | (1 << j)], dp[mask] + (A[i] ^ B[j]));
    }
    cout << dp[(1 << n) - 1] << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n = data[0]
A = data[1 : 1 + n]
B = data[1 + n : 1 + 2 * n]
INF = float("inf")
full = (1 << n) - 1
dp = [INF] * (1 << n)
dp[0] = 0
for mask in range(1 << n):
    cur = dp[mask]
    if cur == INF or mask == full:
        continue
    a = A[bin(mask).count("1")]
    for j in range(n):
        bit = 1 << j
        if not mask & bit:
            v = cur + (a ^ B[j])
            if v < dp[mask | bit]:
                dp[mask | bit] = v
print(dp[full])

Java solution

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        long[] A = new long[n], B = new long[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            A[i] = (long) in.nval;
        }
        for (int i = 0; i < n; i++) {
            in.nextToken();
            B[i] = (long) in.nval;
        }
        long[] dp = new long[1 << n];
        Arrays.fill(dp, Long.MAX_VALUE);
        dp[0] = 0;
        for (int mask = 0; mask < (1 << n); mask++) {
            if (dp[mask] == Long.MAX_VALUE) continue;
            int i = Integer.bitCount(mask);
            if (i == n) continue;
            for (int j = 0; j < n; j++)
                if ((mask & (1 << j)) == 0) dp[mask | (1 << j)] = Math.min(dp[mask | (1 << j)], dp[mask] + (A[i] ^ B[j]));
        }
        System.out.println(dp[(1 << n) - 1]);
    }
}

Solve Minimum XOR Sum of Two Arrays online · All practice problems