# Minimize product of first N – 1 natural numbers by swapping same positioned bits of pairs

• Difficulty Level : Hard
• Last Updated : 12 Apr, 2021

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}
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:

1. Initialize the ans as 1.
2. Iterate over the range [0, 1 + (N – 4)/2].
3. In each traversal, multiply ans with N – 2 and update the ans to ans mod 1e9+7.
4. 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 ``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;``}`

## 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`

## 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`

## C#

 `// C# program for the``// above approach``using` `System;``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``  ``Console.Write(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 Rajput-Ji`

## Javascript

 ``

Output

`1512`

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

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6145694,"math_prob":0.99521303,"size":5184,"snap":"2022-27-2022-33","text_gpt3_token_len":1865,"char_repetition_ratio":0.12702702,"word_repetition_ratio":0.41025642,"special_character_ratio":0.41705248,"punctuation_ratio":0.12942272,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9996275,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-10T10:39:18Z\",\"WARC-Record-ID\":\"<urn:uuid:94f1c816-74dd-4add-8970-ab5ac7187db2>\",\"Content-Length\":\"165230\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:80727080-6aef-4fb5-90a4-1c20bf263441>\",\"WARC-Concurrent-To\":\"<urn:uuid:3f00ab0f-e3f6-4deb-9b2e-aad8dfc5f2b1>\",\"WARC-IP-Address\":\"23.218.218.83\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/minimize-product-of-first-n-1-natural-numbers-by-swapping-same-positioned-bits-of-pairs/\",\"WARC-Payload-Digest\":\"sha1:5LVH52KOGSYSFR2MHCLSN2DHPJSCWVR2\",\"WARC-Block-Digest\":\"sha1:XX2PPNGLMJAGKYJW67TDKWVBK622EC6I\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571153.86_warc_CC-MAIN-20220810100712-20220810130712-00392.warc.gz\"}"}