Given an integer N, the task is to find the minimum positive product of first N – 1 natural numbers, i.e. [1, (N – 1)], by swapping any ith bit of any two numbers any number of times.
Note: N is always a perfect power of 2. Since the product can be very large, print the answer modulo 109 + 7.
Examples:
Input: N = 4
Output: 6
Explanation:
No swapping of bits is required. Therefore, the minimum product is 1*2*3 = 6.
Input: N = 8
Output: 1512
Explanation:
Let the array arr[] stores all the value from 1 to N as {1, 2, 3, 4, 5, 6, 7}
Follow the below steps:
Step 1: In elements 2 = (0010) and 5 = (0101), swap 0th and 1st bit. Therefore, replace 2 with 1 and 5 with 6. arr[] = {1, 1, 3, 4, 6, 6, 7}.
Step 2: In elements 3 = (0011) and 4 = (0100), swap 1th bit. Therefore, replace 3 with 1 and 4 with 6. arr[] = {1, 1, 1, 6, 6, 6, 7}.
Hence, the minimum product = 1*1*1*6*6*6*7 = 1512 % 1e9+7 = 1512.
Approach: The idea is to make some observations. For example, if N = 8 and arr[] = {1, 2, 3, 4, 5, 6, 7}, observe that for the product to be minimum there must be three sixes i.e., there must be an element having value (N – 2) with the frequency of occurrence as (1 + (N – 4)/2) and there must be three ones i.e., there must be (1 + (N – 4)/2) ones. And at last multiply the current product with (N – 1). Hence, the formula becomes:
Minimum product for any value N = ((N – 1) * (N – 2)(N – 4)/2 + 1) % 1e9 + 7
Follow the below steps to solve the problem:
- Initialize the ans as 1.
- Iterate over the range [0, 1 + (N – 4)/2].
- In each traversal, multiply ans with N – 2 and update the ans to ans mod 1e9+7.
- After the above steps, print the value of ans*(N – 1) mod 1e9+7 as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
void minProduct( int n)
{
int ans = 1;
for ( int i = 1;
i <= (n - 4) / 2; i++) {
ans = (1LL * ans
* (n - 2))
% mod;
}
ans = (1LL * ans
* (n - 2) * (n - 1))
% mod;
cout << ans << endl;
}
int main()
{
int N = 8;
minProduct(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mod = ( int )1e9 + 7 ;
static void minProduct( int n)
{
int ans = 1 ;
for ( int i = 1 ;
i <= (n - 4 ) / 2 ; i++)
{
ans = ( int )(1L * ans *
(n - 2 )) % mod;
}
ans = ( int )(1L * ans *
(n - 2 ) * (n - 1 )) % mod;
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int N = 8 ;
minProduct(N);
}
}
|
Python3
mod = 1e9 + 7
def minProduct(n):
ans = 1
for i in range ( 1 , (n - 4 ) / / 2 + 1 ):
ans = (ans * (n - 2 )) % mod
ans = (ans * (n - 2 ) * (n - 1 )) % mod
print ( int (ans))
if __name__ = = '__main__' :
N = 8
minProduct(N)
|
C#
using System;
class GFG{
static int mod = ( int )1e9 + 7;
static void minProduct( int n)
{
int ans = 1;
for ( int i = 1;
i <= (n - 4) / 2; i++)
{
ans = ( int )(1L * ans *
(n - 2)) % mod;
}
ans = ( int )(1L * ans *
(n - 2) *
(n - 1)) % mod;
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int N = 8;
minProduct(N);
}
}
|
Javascript
<script>
let mod = 1e9 + 7;
function minProduct(n)
{
let ans = 1;
for (let i = 1; i <= Math.floor((n - 4) / 2); i++)
{
ans = (1 * ans * (n - 2)) % mod;
}
ans = (1 * ans * (n - 2) * (n - 1)) % mod;
document.write(ans + "<br>" );
}
let N = 8;
minProduct(N);
</script>
|
Time Complexity: O(N) where N is the given integer.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Apr, 2021
Like Article
Save Article