We’re preparing your current view and syncing the latest data.
Given two arrays nums1 and nums2 of the same length n, rearrange the elements of nums1 such that the sum of the bitwise XOR of each pair of elements (nums1[i] XOR nums2[i]) is maximized. Return the maximum sum achievable.
Two integer arrays nums1 and nums2 each of length n.
An integer representing the maximum sum of bitwise XOR values after optimal rearrangement of nums1.
1 <= n <= 10^5 0 <= nums1[i], nums2[i] <= 10^9
Example 1
Input
nums1 = [1,2,3] nums2 = [3,2,1]
Output
6
Explanation
One optimal rearrangement is nums1 = [3,2,1]. Then XOR pairs are (3^3=0), (2^2=0), (1^1=0), sum = 0. But choosing nums1 = [2,3,1], XOR pairs are (2^3=1), (3^2=1), (1^1=0), sum = 2. A better rearrangement is nums1 = [1,3,2]. XOR pairs: (1^3=2), (3^2=1), (2^1=3), sum = 6, which is maximum.