You are given a positive integer p. Consider an array nums (1-indexed) that consists of the integers in the inclusive range [1, 2p - 1] in their binary representations. You are allowed to do the following operation any number of times:
x and y from nums.x and swap it with its corresponding bit in y. 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 109 + 7.
Note: The answer should be the minimum product before the modulo operation is done.
Example 1:
Input: p = 1 Output: 1 Explanation: nums = [1]. There is only one element, so the product equals that element.
Example 2:
Input: p = 2 Output: 6 Explanation: nums = [01, 10, 11]. Any swap would either make the product 0 or stay the same. Thus, the array product of 1 * 2 * 3 = 6 is already minimized.
Example 3:
Input: p = 3
Output: 1512
Explanation: nums = [001, 010, 011, 100, 101, 110, 111]
- In the first operation we can swap the leftmost bit of the second and fifth elements.
- The resulting array is [001, 110, 011, 100, 001, 110, 111].
- In the second operation we can swap the middle bit of the third and fourth elements.
- The resulting array is [001, 110, 001, 110, 001, 110, 111].
The array product is 1 * 6 * 1 * 6 * 1 * 6 * 7 = 1512, which is the minimum possible product.
Constraints:
1 <= p <= 60Problem Overview: The array contains every integer from 1 to 2^p - 1. You can swap bits between numbers any number of times. The goal is to produce the smallest possible non-zero product of all elements after performing optimal swaps.
Approach 1: Iterative Product Calculation Using Simulation (O(2^p) time, O(1) space)
A direct way to reason about the result is to simulate how numbers pair together after optimal swaps. The largest number (2^p - 1) stays unchanged because reducing it increases the overall product cost. All other values tend to form pairs that become (2^p - 2) and 1. If you simulate this pattern, you repeatedly multiply the value (2^p - 2) for each pair while keeping one copy of (2^p - 1). This approach mirrors the greedy transformation but computes the product step by step, applying modulo 1e9+7. It works for understanding the structure but becomes infeasible for large p because the array size grows exponentially.
Approach 2: Optimized Mathematical Approach Using Powers (O(log MOD) time, O(1) space)
The key observation is mathematical. After optimal swaps, the minimal configuration keeps one maximum value (2^p - 1), while the remaining numbers form (2^(p-1) - 1) pairs whose effective value becomes (2^p - 2). This leads to a closed-form product: (2^p - 1) * (2^p - 2)^(2^(p-1) - 1). Since the exponent can be extremely large, compute the power using fast modular exponentiation (binary exponentiation). This reduces the exponentiation cost to logarithmic time. The logic relies on insights from math, a greedy observation about pairing values via greedy optimization, and efficient exponentiation that can be implemented recursively or iteratively using ideas from recursion. This is the expected interview solution because it avoids constructing the array entirely.
Recommended for interviews: Interviewers expect the mathematical insight combined with fast exponentiation. Showing the simulation reasoning demonstrates understanding of the pairing pattern, but deriving the formula and computing it in O(log MOD) time shows stronger algorithmic thinking.
This approach reduces the problem to mathematical operations that leverage properties of numbers and their order. We focus on the most significant arrangements to reduce the elements' product by rearranging their bits optimally using the provided conditions.
In Python, we compute the minimum non-zero product by focusing on the largest number 2^p - 1 and using it in combination with its prior values. We utilize the modulo operation to keep numbers bounded, leveraging Python’s pow function to handle modular exponentiation efficiently.
Python
JavaScript
C
Time Complexity: O(log(p)) due to the modular exponentiation.
Space Complexity: O(1) as we're using a constant amount of space.
This approach simulates the bit manipulation and generates the minimal product iteratively using properties of bit-level operations. The idea is to have systematic element pairings that create complementary binary values to minimize the product effectively.
The Java solution involves simulating the mathematical deduction using a helper function, quickPow, for efficiently calculating powers under modulo. This replicates the complexity handling from other languages using iterative control structures.
Time Complexity: O(log(p)) due to the power function.
Space Complexity: O(1).
We notice that each operation does not change the sum of the elements. When the sum of the elements remains unchanged, to minimize the product, we should maximize the difference between the elements as much as possible.
Since the largest element is 2^p - 1, no matter which element it exchanges with, it will not increase the difference. Therefore, we do not need to consider the case of exchanging with the largest element.
For the other elements in [1,..2^p-2], we pair the first and last elements one by one, that is, pair x with 2^p-1-x. After several operations, each pair of elements becomes (1, 2^p-2). The final product is (2^p-1) times (2^p-2)^{2^{p-1}-1}.
The time complexity is O(p), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Optimized Mathematical Approach Using Powers | Time Complexity: O(log(p)) due to the modular exponentiation. |
| Iterative Product Calculation Using Simulation | Time Complexity: O(log(p)) due to the power function. |
| Greedy + Fast Power | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Product Calculation Using Simulation | O(2^p) | O(1) | Useful for understanding how greedy pair transformations produce the minimal product |
| Optimized Mathematical Approach Using Powers | O(log MOD) | O(1) | Best for production and interviews when p is large |
Leetcode : Minimum Non-Zero Product of the Array Elements • Coding For Dummies • 3,665 views views
Watch 4 more video solutions →Practice Minimum Non-Zero Product of the Array Elements with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor