📌  相关文章
📜  生成 [1, N] 的排列,其中相邻差的按位异或为 0

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

生成 [1, N] 的排列,其中相邻差的按位异或为 0

给定一个整数N ,任务是生成从 1 到 N 的排列,使得相邻元素之间差异的按位 XOR 为 0,即 | A[1]− A[2] | ^ | A[2]− A[3] | ^ . . . ^ | A[N -1] - A[N] | = 0,其中 |X – Y|表示 X 和 Y 之间的绝对差。

例子:

方法:这个问题可以根据以下观察来解决:

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

  • 如果 N 为奇数,则将所有 N 个元素按排序方式排列,因为任意两个相邻元素之间的差为 1,且相邻差的个数为偶数。
  • 如果 N 是偶数:
    • 保留 2、3、1、4 作为前四个元素,因为 3 个差异具有 XOR 0。
    • 现在从 5 开始并按排序顺序打印剩余的元素,这将使所有剩余的偶数差异的差异为 1。

下面是上述方法的实现。

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to print shuffle array
vector shuffleArray(int n)
{
    vector res;
 
    // Base case
    if (n < 3)
        cout << -1 << endl;
 
    // When n is odd print array in
    // increasing order
    else if (n % 2 != 0) {
        for (int i = 1; i <= n; i++)
            res.push_back(i);
    }
 
    // When n is even print first 2 3 1 4
    // rest element in increasing order
    else {
        res = { 2, 3, 1, 4 };
        for (int i = 5; i <= n; i++)
            res.push_back(i);
    }
    return res;
}
 
// Driver code
int main()
{
    int N = 4;
 
    vector ans = shuffleArray(N);
    for (int x : ans)
        cout << x << " ";
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
public class GFG {
 
  // Function to print shuffle array
  static ArrayList shuffleArray(int n)
  {
    ArrayList res = new ArrayList();
 
    // Base case
    if (n < 3)
      System.out.println(-1);
 
    // When n is odd print array in
    // increasing order
    else if (n % 2 != 0) {
      for (int i = 1; i <= n; i++)
        res.add(i);
    }
 
    // When n is even print first 2 3 1 4
    // rest element in increasing order
    else {
      res.clear();
 
      res.add(2);
      res.add(3);
      res.add(1);
      res.add(4);
 
      for (int i = 5; i <= n; i++)
        res.add(i);
    }
    return res;
  }
 
  // Driver code
  public static void main(String args[])
  {
    int N = 4;
 
    ArrayList ans = shuffleArray(N);
    for (int i = 0; i < ans.size(); i++)
      System.out.print(ans.get(i) + " ");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python3 implementation of above approach
 
# Function to print shuffle array
def shuffleArray(n):
 
    res = []
 
    # Base case
    if (n < 3):
        print(-1)
 
    # When n is odd print array in
    # increasing order
    elif (n % 2 != 0):
        for i in range(1, n):
            res.append(i)
 
    # When n is even print first 2 3 1 4
    # rest element in increasing order
    else:
        res = [2, 3, 1, 4]
        for i in range(5, n):
            res.append(i)
 
    return res
 
# Driver code
if __name__ == '__main__':
    n = 4
    res = shuffleArray(n)
    for i in res:
        print(i, end=' ')
 
        # This code is contributed by richasalan57.


C#
// C# implementation of above approach
using System;
using System.Collections;
 
class GFG
{
 
// Function to print shuffle array
static ArrayList shuffleArray(int n)
{
    ArrayList res = new ArrayList();
 
    // Base case
    if (n < 3)
        Console.WriteLine(-1);
 
    // When n is odd print array in
    // increasing order
    else if (n % 2 != 0) {
        for (int i = 1; i <= n; i++)
            res.Add(i);
    }
 
    // When n is even print first 2 3 1 4
    // rest element in increasing order
    else {
        res.Clear();
         
        res.Add(2);
        res.Add(3);
        res.Add(1);
        res.Add(4);
         
        for (int i = 5; i <= n; i++)
            res.Add(i);
    }
    return res;
}
 
// Driver code
public static void Main()
{
    int N = 4;
 
    ArrayList ans = shuffleArray(N);
    foreach (int x in ans)
        Console.Write(x + " ");
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
2 3 1 4 

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