📌  相关文章
📜  通过任意对任意次数交换位来最小化前 2^K–1 个自然数的乘积

📅  最后修改于: 2022-05-13 01:56:04.763000             🧑  作者: Mango

通过任意对任意次数交换位来最小化前 2^K–1 个自然数的乘积

给定一个正整数K 任务是通过任意次数交换任意两个数的对应位置的位来最小化前(2 K – 1) 个自然数的正积。

例子:

方法:给定的问题可以基于以下观察来解决:为了获得最小的正积,数字1的频率应该通过交换任意两个数字的位来最大。请按照以下步骤解决给定的问题:

  • 找到范围的值(2 K – 1)
  • 通过将除第0之外的所有奇数位转换为偶数,将range/2元素转换为 1。
  • 因此, range/2数字将为 1 , range/2数字将为range – 1 ,并且数组中的最后一个数字将与range的值相同。
  • 求上述步骤中形成的所有数的合成乘积,作为得到的最小正乘积。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum positive
// product after swapping bits at the
// similar position for any two numbers
int minimumPossibleProduct(int K)
{
    // Stores the resultant number
    int res = 1;
 
    int range = (1 << K) - 1;
 
    // range/2 numbers will be 1 and
    // range/2 numbers will be range-1
    for (int i = 0; i < K; i++) {
        res *= (range - 1);
    }
 
    // Multiply with the final number
    res *= range;
 
    // Return the answer
    return res;
}
// Driver Code
int main()
{
    int K = 3;
    cout << minimumPossibleProduct(K);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
   
// Function to find the minimum positive
// product after swapping bits at the
// similar position for any two numbers
static int minimumPossibleProduct(int K)
{
    // Stores the resultant number
    int res = 1;
 
    int range = (1 << K) - 1;
 
    // range/2 numbers will be 1 and
    // range/2 numbers will be range-1
    for (int i = 0; i < K; i++) {
        res *= (range - 1);
    }
 
    // Multiply with the final number
    res *= range;
 
    // Return the answer
    return res;
}
   
// Driver Code
public static void main (String[] args) {
   
    int K = 3;
    System.out.println(minimumPossibleProduct(K));
}
}
 
// This code is contributed by Dharanendra L V.


Python3
# python program for the above approach
 
# Function to find the minimum positive
# product after swapping bits at the
# similar position for any two numbers
def minimumPossibleProduct(K):
   
    # Stores the resultant number
    res = 1
    r = (1 << K) - 1
     
    # range/2 numbers will be 1 and
    # range/2 numbers will be range-1
    for i in range(0, K):
        res *= (r - 1)
 
    # Multiply with the final number
    res *= r
 
    # Return the answer
    return res
 
# Driver Code
K = 3
print(minimumPossibleProduct(K))
 
# This code is contributed by amreshkumar3.


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to find the minimum positive
    // product after swapping bits at the
    // similar position for any two numbers
    static int minimumPossibleProduct(int K)
    {
       
        // Stores the resultant number
        int res = 1;
 
        int range = (1 << K) - 1;
 
        // range/2 numbers will be 1 and
        // range/2 numbers will be range-1
        for (int i = 0; i < K; i++) {
            res *= (range - 1);
        }
 
        // Multiply with the final number
        res *= range;
 
        // Return the answer
        return res;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        int K = 3;
        Console.WriteLine(minimumPossibleProduct(K));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
1512

时间复杂度: O(K)
辅助空间: O(1)