Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given two integer arrays nums1 and nums2 of length n.
The XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1]) (0-indexed).
[1,2,3] and [3,2,1] is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4.Rearrange the elements of nums2 such that the resulting XOR sum is minimized.
Return the XOR sum after the rearrangement.
Example 1:
Input: nums1 = [1,2], nums2 = [2,3] Output: 2 Explanation: Rearrangenums2so that it becomes[3,2]. The XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.
Example 2:
Input: nums1 = [1,0,3], nums2 = [5,3,4] Output: 8 Explanation: Rearrangenums2so that it becomes[5,4,3]. The XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.
Constraints:
n == nums1.lengthn == nums2.length1 <= n <= 140 <= nums1[i], nums2[i] <= 107Problem summary: You are given two integer arrays nums1 and nums2 of length n. The XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1]) (0-indexed). For example, the XOR sum of [1,2,3] and [3,2,1] is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4. Rearrange the elements of nums2 such that the resulting XOR sum is minimized. Return the XOR sum after the rearrangement.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[1,2] [2,3]
[1,0,3] [5,3,4]
fair-distribution-of-cookies)choose-numbers-from-two-arrays-in-range)maximum-and-sum-of-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1879: Minimum XOR Sum of Two Arrays
class Solution {
public int minimumXORSum(int[] nums1, int[] nums2) {
int n = nums1.length;
int[][] f = new int[n + 1][1 << n];
for (var g : f) {
Arrays.fill(g, 1 << 30);
}
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 1 << n; ++j) {
for (int k = 0; k < n; ++k) {
if ((j >> k & 1) == 1) {
f[i][j]
= Math.min(f[i][j], f[i - 1][j ^ (1 << k)] + (nums1[i - 1] ^ nums2[k]));
}
}
}
}
return f[n][(1 << n) - 1];
}
}
// Accepted solution for LeetCode #1879: Minimum XOR Sum of Two Arrays
func minimumXORSum(nums1 []int, nums2 []int) int {
n := len(nums1)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, 1<<n)
for j := range f[i] {
f[i][j] = 1 << 30
}
}
f[0][0] = 0
for i := 1; i <= n; i++ {
for j := 0; j < 1<<n; j++ {
for k := 0; k < n; k++ {
if j>>k&1 == 1 {
f[i][j] = min(f[i][j], f[i-1][j^(1<<k)]+(nums1[i-1]^nums2[k]))
}
}
}
}
return f[n][(1<<n)-1]
}
# Accepted solution for LeetCode #1879: Minimum XOR Sum of Two Arrays
class Solution:
def minimumXORSum(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums2)
f = [[inf] * (1 << n) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(nums1, 1):
for j in range(1 << n):
for k in range(n):
if j >> k & 1:
f[i][j] = min(f[i][j], f[i - 1][j ^ (1 << k)] + (x ^ nums2[k]))
return f[-1][-1]
// Accepted solution for LeetCode #1879: Minimum XOR Sum of Two Arrays
/**
* [1879] Minimum XOR Sum of Two Arrays
*
* You are given two integer arrays nums1 and nums2 of length n.
* The XOR sum of the two integer arrays is (nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1]) (0-indexed).
*
* For example, the XOR sum of [1,2,3] and [3,2,1] is equal to (1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4.
*
* Rearrange the elements of nums2 such that the resulting XOR sum is minimized.
* Return the XOR sum after the rearrangement.
*
* Example 1:
*
* Input: nums1 = [1,2], nums2 = [2,3]
* Output: 2
* Explanation: Rearrange nums2 so that it becomes [3,2].
* The XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.
* Example 2:
*
* Input: nums1 = [1,0,3], nums2 = [5,3,4]
* Output: 8
* Explanation: Rearrange nums2 so that it becomes [5,4,3].
* The XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.
*
*
* Constraints:
*
* n == nums1.length
* n == nums2.length
* 1 <= n <= 14
* 0 <= nums1[i], nums2[i] <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-xor-sum-of-two-arrays/
// discuss: https://leetcode.com/problems/minimum-xor-sum-of-two-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_xor_sum(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1879_example_1() {
let nums1 = vec![1, 2];
let nums2 = vec![2, 3];
let result = 2;
assert_eq!(Solution::minimum_xor_sum(nums1, nums2), result);
}
#[test]
#[ignore]
fn test_1879_example_2() {
let nums1 = vec![1, 0, 3];
let nums2 = vec![5, 3, 4];
let result = 8;
assert_eq!(Solution::minimum_xor_sum(nums1, nums2), result);
}
}
// Accepted solution for LeetCode #1879: Minimum XOR Sum of Two Arrays
function minimumXORSum(nums1: number[], nums2: number[]): number {
const n = nums1.length;
const f: number[][] = Array(n + 1)
.fill(0)
.map(() => Array(1 << n).fill(1 << 30));
f[0][0] = 0;
for (let i = 1; i <= n; ++i) {
for (let j = 0; j < 1 << n; ++j) {
for (let k = 0; k < n; ++k) {
if (((j >> k) & 1) === 1) {
f[i][j] = Math.min(f[i][j], f[i - 1][j ^ (1 << k)] + (nums1[i - 1] ^ nums2[k]));
}
}
}
}
return f[n][(1 << n) - 1];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.