📜  生成具有最大相邻 XOR 的 [0, N-1] 排列,这是其他排列中最小的

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

生成具有最大相邻 XOR 的 [0, N-1] 排列,这是其他排列中最小的

给定一个整数N ,任务是打印从0 到 N-1的数字排列,这样:

  • 排列中没有重复元素
  • 此排列的最大相邻XOR在其他排列中是最小的
  • 可以存在多个满足这些条件的排列。

例子:

方法:解决这个问题的直觉是基于下面提到的 XOR 属性

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

  • 最初,计算具有最高有效设置位的小于或等于N-1的最小数。
  • 从 1 开始连续打印所有小于具有最高有效设置位的最小数字的数字。
  • 打印 0。
  • 打印从具有最高有效设置位的最小数字开始的所有数字,直到N-1

下面是上述方法的实现:

C++
// C++ program to implement above approach
#include 
using namespace std;
 
// Function to get the desired permutation
void getPermutation(int n)
{
    // Calculate the maximum number
    // in the permutation
    int maxnum = n - 1;
 
    // Calculate the minimum number
    // bit is 1 where most significant
    int num = 1;
    while (maxnum > 0) {
        maxnum /= 2;
        num *= 2;
    }
    num /= 2;
 
    // Print all numbers less than the number
    // where most significant bit is set
    for (int i = 1; i <= num - 1; i++) {
        cout << i << " ";
    }
 
    // Print 0
    cout << 0 << " ";
 
    // Print all the numbers
    // greater than or equal to
    // the number where
    // most significant bit is set
    for (int i = num; i < n; i++) {
        cout << i << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
   
    // Function call
    getPermutation(N);
    return 0;
}


Java
// JAVA program to implement above approach
import java.util.*;
class GFG
{
   
    // Function to get the desired permutation
    public static void getPermutation(int n)
    {
       
        // Calculate the maximum number
        // in the permutation
        int maxnum = n - 1;
 
        // Calculate the minimum number
        // bit is 1 where most significant
        int num = 1;
        while (maxnum > 0) {
            maxnum /= 2;
            num *= 2;
        }
        num /= 2;
 
        // Print all numbers less than the number
        // where most significant bit is set
        for (int i = 1; i <= num - 1; i++) {
            System.out.print(i + " ");
        }
 
        // Print 0
        System.out.print(0 + " ");
 
        // Print all the numbers
        // greater than or equal to
        // the number where
        // most significant bit is set
        for (int i = num; i < n; i++) {
            System.out.print(i + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
 
        // Function call
        getPermutation(N);
    }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
 
# Function to get the desired permutation
def getPermutation(n):
 
    # Calculate the maximum number
    # in the permutation
    maxnum = n - 1
 
    # Calculate the minimum number
    # bit is 1 where most significant
    num = 1
    while (maxnum > 0):
        maxnum = maxnum//2
        num *= 2
 
    num = (num//2)
 
    # Print all numbers less than the number
    # where most significant bit is set
    for i in range(1, num):
        print(i, end=" ")
 
    # Print 0
    print(0, end=" ")
 
    # Print all the numbers
    # greater than or equal to
    # the number where
    # most significant bit is set
    for i in range(num, n):
        print(i, end=" ")
 
# Driver Code
N = 5
 
# Function call
getPermutation(N)
 
# This code is contributed by Potta Lokesh


C#
// C# program to implement above approach
using System;
class GFG
{
   
    // Function to get the desired permutation
    static void getPermutation(int n)
    {
       
        // Calculate the maximum number
        // in the permutation
        int maxnum = n - 1;
 
        // Calculate the minimum number
        // bit is 1 where most significant
        int num = 1;
        while (maxnum > 0) {
            maxnum /= 2;
            num *= 2;
        }
        num /= 2;
 
        // Print all numbers less than the number
        // where most significant bit is set
        for (int i = 1; i <= num - 1; i++) {
            Console.Write(i + " ");
        }
 
        // Print 0
        Console.Write(0 + " ");
 
        // Print all the numbers
        // greater than or equal to
        // the number where
        // most significant bit is set
        for (int i = num; i < n; i++) {
            Console.Write(i + " ");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 5;
 
        // Function call
        getPermutation(N);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1 2 3 0 4 

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