📌  相关文章
📜  相邻对的最小可能最大异或的排列计数

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

相邻对的最小可能最大异或的排列计数

给定一个整数N ,考虑一个数组,其元素在[0, N-1]范围内,使得所有相邻对的最大按位异或在数组的所有可能排列中最小。找出这种排列的数量。

例子:

方法:如果所有元素都以它们的二进制表示形式编写,则可以根据谁拥有最大可能的最左设置位集的条件将它们分为两组。因此,一组将具有最大可能的最左边位设置的元素,而另一组将包含剩余的元素。以下是解决问题的主要观察:

按照下面提到的步骤来实现上述观察:

  • 找到最大可能的最左边 setbit
  • 现在计算每个组中的元素数量
  • 找到密钥对
  • 计算每边的排列,然后将它们相互乘以2
  • 返回这个相乘的值作为答案。

下面是上述算法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find factorial of a number
int factorial(int x)
{
    int ans = 1;
    while (x > 0) {
        ans = ans * x;
        x--;
    }
    return ans;
}
 
// Function to find the MSB of a number
int mostSigBit(int x)
{
    int msb = -1;
    while (x != 0) {
        x = x >> 1;
        msb++;
    }
    return msb;
}
 
// Function to calculate number of possible
// permutations with minimum bitwise XOR
int minXor(int N)
{
    int highest_bit = mostSigBit(N - 1);
 
    // The highest power of 2 before
    // the largest number which will
    // be part of the key pair with 0
    int key = 1 << highest_bit;
 
    // Count of elements in group 1
    int grp1 = 0;
 
    // Count of elements in group 2
    int grp2 = 0;
 
    for (int i = 1; i < N; i++) {
        if (i > key)
            grp2++;
        else if (i < key)
            grp1++;
    }
 
    int ans = (factorial(grp1)
               * factorial(grp2))
              * 2;
    return ans;
}
 
// Driver code
int main()
{
    int N = 5;
 
    // Function call
    int ans = minXor(N);
    cout << ans;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
public class GFG {
 
    // Function to find factorial of a number
    static int factorial(int x)
    {
        int ans = 1;
        while (x > 0) {
            ans = ans * x;
            x--;
        }
        return ans;
    }
 
    // Function to find the MSB of a number
    static int mostSigBit(int x)
    {
        int msb = -1;
        while (x != 0) {
            x = x >> 1;
            msb++;
        }
        return msb;
    }
 
    // Function to calculate number of possible
    // permutations with minimum bitwise XOR
    static int minXor(int N)
    {
        int highest_bit = mostSigBit(N - 1);
 
        // The highest power of 2 before the
        // largest number which will be
        // part of the key pair with 0
        int key = 1 << highest_bit;
 
        // Count of elements in group 1
        int grp1 = 0;
 
        // Count of elements in group 2
        int grp2 = 0;
 
        for (int i = 1; i < N; i++) {
            if (i > key)
                grp2++;
            else if (i < key)
                grp1++;
        }
 
        int ans = (factorial(grp1)
                   * factorial(grp2))
                  * 2;
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 5;
 
        // Function call
        int ans = minXor(N);
        System.out.println(ans);
    }
}


Python3
# Python code for the above approach
 
# Function to find factorial of a number
def factorial(x):
    ans = 1;
    while (x > 0):
        ans = ans * x;
        x -= 1
 
    return ans;
 
# Function to find the MSB of a number
def mostSigBit(x):
    msb = -1;
    while (x != 0):
        x = x >> 1;
        msb += 1
     
    return msb;
 
# Function to calculate number of possible
# permutations with minimum bitwise XOR
def minXor(N):
    highest_bit = mostSigBit(N - 1);
 
    # The highest power of 2 before
    # the largest number which will
    # be part of the key pair with 0
    key = 1 << highest_bit;
 
    # Count of elements in group 1
    grp1 = 0;
 
    # Count of elements in group 2
    grp2 = 0;
 
    for i in range(1, N) :
        if (i > key):
            grp2 += 1
        elif (i < key):
            grp1 += 1
     
    ans = (factorial(grp1) * factorial(grp2)) * 2
    return ans;
 
# Driver code
N = 5;
 
# Function call
ans = minXor(N);
print(ans);
 
# This code is contributed by Saurabh jaiswal


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find factorial of a number
  static int factorial(int x)
  {
    int ans = 1;
    while (x > 0) {
      ans = ans * x;
      x--;
    }
    return ans;
  }
 
  // Function to find the MSB of a number
  static int mostSigBit(int x)
  {
    int msb = -1;
    while (x != 0) {
      x = x >> 1;
      msb++;
    }
    return msb;
  }
 
  // Function to calculate number of possible
  // permutations with minimum bitwise XOR
  static int minXor(int N)
  {
    int highest_bit = mostSigBit(N - 1);
 
    // The highest power of 2 before the
    // largest number which will be
    // part of the key pair with 0
    int key = 1 << highest_bit;
 
    // Count of elements in group 1
    int grp1 = 0;
 
    // Count of elements in group 2
    int grp2 = 0;
 
    for (int i = 1; i < N; i++) {
      if (i > key)
        grp2++;
      else if (i < key)
        grp1++;
    }
 
    int ans = (factorial(grp1)
               * factorial(grp2))
      * 2;
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 5;
 
    // Function call
    int ans = minXor(N);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出:
12

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