BackhardBacktrackingMicrosoft

Count of Safe XOR Permutations Solution

Problem Statement

You are given an integer array nums, which may contain duplicate elements, and an integer array banned. A permutation of nums is considered "safe" if the bitwise XOR sum of any of its prefixes is not present in the banned array. Return the total number of unique safe permutations of nums. Since the array nums can contain duplicate values, you must only count distinct permutations.

Example 1
Input
[1, 2, 3], [0, 3]
Output
0

Explanation: Step-by-step: We have three elements [1, 2, 3] and two banned numbers [0, 3]. The XOR sum of any prefix of the permutation [1, 2, 3] will always be present in the banned array {0, 3}. Therefore, the total number of unique safe permutations of nums is 0.

Example 2
Input
[1], []
Output
1

Explanation: Step-by-step: We have one element [1] and no banned numbers. The permutation [1] is safe. Therefore, the total number of unique safe permutations of nums is 1.

Constraints

  • 1 <= nums.length <= 11
  • 0 <= nums[i] <= 10^6
  • 1 <= banned.length <= 10^5
  • 0 <= banned[i] <= 10^6
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Count of Safe XOR Permutations — Problem Statement & Solution Guide

BacktrackingHardSubsets/Permutations
TimeO(n! * 2^n)
|
SpaceO(n)

Problem Description

You are given an integer array nums, which may contain duplicate elements, and an integer array banned. A permutation of nums is considered "safe" if the bitwise XOR sum of any of its prefixes is not present in the banned array. Return the total number of unique safe permutations of nums. Since the array nums can contain duplicate values, you must only count distinct permutations.

Examples

Example 1

Input

[1, 2, 3], [0, 3]

Output

0

Explanation: Step-by-step: We have three elements [1, 2, 3] and two banned numbers [0, 3]. The XOR sum of any prefix of the permutation [1, 2, 3] will always be present in the banned array {0, 3}. Therefore, the total number of unique safe permutations of nums is 0.

Example 2

Input

[1], []

Output

1

Explanation: Step-by-step: We have one element [1] and no banned numbers. The permutation [1] is safe. Therefore, the total number of unique safe permutations of nums is 1.

Constraints

  • 1 <= nums.length <= 11
  • 0 <= nums[i] <= 10^6
  • 1 <= banned.length <= 10^5
  • 0 <= banned[i] <= 10^6

Optimal Approach & Strategy

Use backtracking with a frequency map to generate unique permutations directly. Maintain the current prefix XOR value and prune branches immediately if the current XOR sum is in the banned set, achieving O(N!) complexity with efficient state pruning.

Brute Force Approach

Generate all possible permutations of the array using backtracking, then verify each permutation by calculating every prefix XOR sum. Filter out permutations that contain a banned value.

Verified Code Solutions

JavaScript Solution
Time: O(n! * 2^n)
function countSafeXorPermutations(nums, banned) {
  const bannedSet = new Set(banned);
  const counts = new Map();
  for (const n of nums) counts.set(n, (counts.get(n) || 0) + 1);
  const uniqueNums = [...new Set(nums)].sort((a, b) => a - b);
  function backtrack(currXor, remaining) {
    if (remaining === 0) return 1;
    let count = 0;
    for (const num of uniqueNums) {
      if (counts.get(num) > 0) {
        const nextXor = currXor ^ num;
        if (!bannedSet.has(nextXor)) {
          counts.set(num, counts.get(num) - 1);
          count += backtrack(nextXor, remaining - 1);
          counts.set(num, counts.get(num) + 1);
        }
      }
    }
    return count;
  }
  let safeCount = 0;
  for (const num of uniqueNums) {
    const countsCopy = new Map(counts);
    countsCopy.set(num, countsCopy.get(num) - 1);
    safeCount += backtrack(0, uniqueNums.length - 1);
    countsCopy.set(num, countsCopy.get(num) + 1);
  }
  return safeCount;
}

Asked in Top Tech Interviews

Microsoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.