Here is the full solution for minimum XOR sum:
plaintext function minXorSum(nums1, nums2) n := length of nums1 INF := large number dp := array of size 2^n, filled with INF dp[0] := 0 for mask := 0 to 2^n - 1 if dp[mask] = INF then continue k := popcount(mask) if k >= n then continue for j := 0 to n - 1 if (mask >> j) & 1 = 1 then continue newMask := mask | (1 << j) cost := nums1[k] XOR nums2[j] dp[newMask] := min return dp[2^n - 1]
The key trick: popcount(mask) tells you how many elements of nums1 have been assigned. If the mask has k bits set, you are assigning nums1[k] next. This eliminates the need for a second dimension in the DP.
Time: O(n⋅2n). Space: O(2n).
Time: O(n2⋅2n). Space: O(n⋅2n).