[LeetCode] Minimum Non-Zero Product of the Array Elements

1969. Minimum Non-Zero Product of the Array Elements

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:

  • Choose two elements x and y from nums.
  • Choose a bit in 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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
int modPow(unsigned long long n,unsigned long long p, int m = 1e9 + 7) {
if(!p) return 1;
unsigned long long res = modPow(n, p>>1, m);
return p & 1 ? (res * res % m) * (n % m) % m : res * res % m;
}
public:
int minNonZeroProduct(int p) {
unsigned long long n = (1ll << p) - 1, m = 1e9 + 7;
return n % m * modPow(n - 1, n >> 1, m) % m;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/12/19/PS/LeetCode/minimum-non-zero-product-of-the-array-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.