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 **i**^{th} 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 **10 ^{9} + 7**.

**Examples:**

Input:N = 4Output:6Explanation:

No swapping of bits is required. Therefore, the minimum product is 1*2*3 = 6.

Input:N = 8Output:1512Explanation:

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++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `mod = 1e9 + 7;` `// Function to find the minimum product` `// of 1 to N - 1 after performing the` `// given operations` `void` `minProduct(` `int` `n)` `{` ` ` `// Initialize ans with 1` ` ` `int` `ans = 1;` ` ` `// Multiply ans with N-2` ` ` `// ((N - 4)/2) times` ` ` `for` `(` `int` `i = 1;` ` ` `i <= (n - 4) / 2; i++) {` ` ` `ans = (1LL * ans` ` ` `* (n - 2))` ` ` `% mod;` ` ` `}` ` ` `// Multiply ans with N - 1` ` ` `// and N - 2 once` ` ` `ans = (1LL * ans` ` ` `* (n - 2) * (n - 1))` ` ` `% mod;` ` ` `// Print ans` ` ` `cout << ans << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Number N` ` ` `int` `N = 8;` ` ` `// Function Call` ` ` `minProduct(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the ` `// above approach` `import` `java.util.*;` `class` `GFG{` `static` `int` `mod = (` `int` `)1e9 + ` `7` `;` `// Function to find the ` `// minimum product of 1 ` `// to N - 1 after performing ` `// the given operations` `static` `void` `minProduct(` `int` `n)` `{` ` ` `// Initialize ans with 1` ` ` `int` `ans = ` `1` `;` ` ` `// Multiply ans with N-2` ` ` `// ((N - 4)/2) times` ` ` `for` `(` `int` `i = ` `1` `;` ` ` `i <= (n - ` `4` `) / ` `2` `; i++) ` ` ` `{` ` ` `ans = (` `int` `)(1L * ans * ` ` ` `(n - ` `2` `)) % mod;` ` ` `}` ` ` `// Multiply ans with N - 1` ` ` `// and N - 2 once` ` ` `ans = (` `int` `)(1L * ans * ` ` ` `(n - ` `2` `) * (n - ` `1` `)) % mod;` ` ` `// Print ans` ` ` `System.out.print(ans + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given Number N` ` ` `int` `N = ` `8` `;` ` ` `// Function Call` ` ` `minProduct(N);` `}` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `mod ` `=` `1e9` `+` `7` `# Function to find the minimum product` `# of 1 to N - 1 after performing the` `# given operations` `def` `minProduct(n):` ` ` ` ` `# Initialize ans with 1` ` ` `ans ` `=` `1` ` ` `# Multiply ans with N-2` ` ` `# ((N - 4)/2) times` ` ` `for` `i ` `in` `range` `(` `1` `, (n ` `-` `4` `) ` `/` `/` `2` `+` `1` `):` ` ` `ans ` `=` `(ans ` `*` `(n ` `-` `2` `)) ` `%` `mod` ` ` `# Multiply ans with N - 1` ` ` `# and N - 2 once` ` ` `ans ` `=` `(ans ` `*` `(n ` `-` `2` `) ` `*` `(n ` `-` `1` `)) ` `%` `mod` ` ` `# Print ans` ` ` `print` `(` `int` `(ans))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given number N` ` ` `N ` `=` `8` ` ` `# Function call` ` ` `minProduct(N)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

**Output**

1512

**Time Complexity:** O(N) where N is the given integer.**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print numbers having first and last bits as the only set bits
- Number of ways to change the XOR of two numbers by swapping the bits
- Check if product of first N natural numbers is divisible by their sum
- Product of all Subsets of a set formed by first N natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Count of pairs with sum N from first N natural numbers
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Check if all bits can be made same by flipping two consecutive bits
- Toggle bits of a number except first and last bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Generate an Array such with elements maximized through swapping bits
- Count of pairs in an Array with same number of set bits
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Maximize product of same-indexed elements of same size subsequences
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.