Minimize product of first 2^K–1 Natural Numbers by swapping bits for any pair any number of times
Last Updated :
18 Oct, 2021
Given a positive integer K, the task is to minimize the positive product of the first (2K – 1) Natural Numbers by swapping the bits at the corresponding position of any two numbers any number of times.
Examples:
Input: K = 3
Output: 1512
Explanation: The original product is 5040. The given array in binary notation is {001, 010, 011, 100, 101, 110, 111}
- In the first operation 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 swap the middle bit of the third and fourth elements. The resulting array is [001, 110, 001, 110, 001, 110, 111].
After the above operations, the array elements are {1, 6, 1, 6, 1, 6, 7}. Therefore, the product is 1 * 6 * 1 * 6 * 1 * 6 * 7 = 1512, which is the minimum possible positive product.
Input: K = 2
Output: 6
Explanation: The original permutation in binary notation is {’00’, ’01’, ’10’}. Any swap will lead to product zero or does not change at all. Hence 6 is the correct output.
Approach: The given problem can be solved based on the observation that to get the minimum positive product the frequency of the number 1 should be maximum by swapping the bits of any two numbers. Follow the steps below to solve the given problem:
- Find the value of the range as (2K – 1).
- Convert range/2 elements to 1 by shifting all bits of odd numbers to even numbers except the 0th bit.
- Therefore, range/2 numbers will be 1 and range/2 numbers will be range – 1, and the last number in the array will remain the same as the value of the range.
- Find the resultant product of all the numbers formed in the above step as the resultant minimum positive product obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumPossibleProduct( int K)
{
int res = 1;
int range = (1 << K) - 1;
for ( int i = 0; i < K; i++) {
res *= (range - 1);
}
res *= range;
return res;
}
int main()
{
int K = 3;
cout << minimumPossibleProduct(K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minimumPossibleProduct( int K)
{
int res = 1 ;
int range = ( 1 << K) - 1 ;
for ( int i = 0 ; i < K; i++) {
res *= (range - 1 );
}
res *= range;
return res;
}
public static void main (String[] args) {
int K = 3 ;
System.out.println(minimumPossibleProduct(K));
}
}
|
Python3
def minimumPossibleProduct(K):
res = 1
r = ( 1 << K) - 1
for i in range ( 0 , K):
res * = (r - 1 )
res * = r
return res
K = 3
print (minimumPossibleProduct(K))
|
C#
using System;
class GFG {
static int minimumPossibleProduct( int K)
{
int res = 1;
int range = (1 << K) - 1;
for ( int i = 0; i < K; i++) {
res *= (range - 1);
}
res *= range;
return res;
}
public static void Main( string [] args)
{
int K = 3;
Console.WriteLine(minimumPossibleProduct(K));
}
}
|
Javascript
<script>
function minimumPossibleProduct(K)
{
let res = 1;
let range = (1 << K) - 1;
for (let i = 0; i < K; i++) {
res *= (range - 1);
}
res *= range;
return res;
}
let K = 3;
document.write(minimumPossibleProduct(K));
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...