Minimum Non-Zero Product of the Array Elements - Problem
You are given a positive integer p. Consider an array nums (1-indexed) that consists of the integers in the inclusive range [1, 2^p - 1] in their binary representations. You are allowed to do the following operation any number of times:
- Choose two elements
xandyfromnums. - Choose a bit in
xand swap it with its corresponding bit iny. Corresponding bit refers to the bit that is in the same position in the other integer.
For example, if x = 1101 and y = 0011, after swapping the 2nd bit from the right, we have x = 1111 and y = 0001.
Find the minimum non-zero product of nums after performing the above operation any number of times. Return this product modulo 10^9 + 7.
Note: The answer should be the minimum product before the modulo operation is done.
Input & Output
Example 1 — Basic Case
$
Input:
p = 1
›
Output:
1
💡 Note:
Array is [1], so the product is 1
Example 2 — Small Array
$
Input:
p = 2
›
Output:
6
💡 Note:
Array is [1,2,3]. Optimal: swap to get [1,2,3] or [3,2,1]. Product = 1×2×3 = 6
Example 3 — Larger Case
$
Input:
p = 3
›
Output:
1512
💡 Note:
Array is [1,2,3,4,5,6,7]. After optimal swaps: many 1s, many 6s, one 7. Product = 6³ × 7 = 1512
Constraints
- 1 ≤ p ≤ 60
Visualization
Tap to expand
Understanding the Visualization
1
Input
Integer p defines range [1, 2^p-1]
2
Bit Swaps
Rearrange bits to minimize product
3
Output
Minimum product modulo 10^9+7
Key Takeaway
🎯 Key Insight: Minimize product by creating many 1s through optimal bit distribution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code