Bit Manipulation - Generate All Permutations Using Bitmask DP
Consider the following code that generates all permutations of 3 elements using bitmask DP with iterative tabulation. What is the content of
dp[7] after the loops finish?def generate_permutations_iterative(n):
dp = [[] for _ in range(1 << n)]
dp[0] = [[]]
for mask in range(1 << n):
for i in range(n):
if not (mask & (1 << i)):
for perm in dp[mask]:
dp[mask | (1 << i)].append(perm + [i])
return dp[(1 << n) - 1]
perms = generate_permutations_iterative(3)
print(perms)