生成具有最大相邻 XOR 的 [0, N-1] 排列,这是其他排列中最小的
给定一个整数N ,任务是打印从0 到 N-1的数字排列,这样:
- 排列中没有重复元素
- 此排列的最大相邻XOR在其他排列中是最小的
- 可以存在多个满足这些条件的排列。
例子:
Input: N = 5
Output: 1 2 3 0 4
Explanation:
The maximum XOR value of two consecutive elements of the permutation is 0 XOR 4 = 4
No other permutation exists where the maximum XOR value of two consecutive elements of the permutation is less than 4.
Other permutations may exist where the value is equal to or greater than 4.
Input: N = 10
Output: 1 2 3 4 5 6 7 0 8 9
方法:解决这个问题的直觉是基于下面提到的 XOR 属性:
- Inside the entire permutation, there will be some numbers with the most significant bit as 1 or as 0.
- So group the numbers with the most significant bit as 1 so the most significant bit gets cancelled out.
- But there will always be a case in the permutation where a number with the most significant bit as 1 will reside before or after a number where the most significant bit is 0. At that point, the most significant bit will not be cancelled by the XOR operation.
- In that case, place the minimum number with the most significant bit as 1 and the minimum number with the most significant bit as 0(possibly 0), together to get the maximum XOR value.
- This is the possible minimum value of maximum-XOR value among all permutations.
按照下面提到的步骤来实现上述观察:
- 最初,计算具有最高有效设置位的小于或等于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)