📌  相关文章
📜  生成一个循环排列,相邻元素对之间的不匹配位数恰好为 1

📅  最后修改于: 2021-09-07 03:00:45             🧑  作者: Mango

给定两个整数NS ,任务是从[0, 2 (N – 1) ]范围内找到数字的循环排列,从S开始,使得任何一对相邻数字之间的不匹配位计数为1

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个列表,比如res ,以存储所需的排列。
  • 初始化一个整数,比如index ,以存储S在从0开始的排列中的位置。
  • 迭代范围[0, N – 1]并以相反的顺序遍历数组res[]并检查当前数字2 i的总和是否为S。如果发现为真,则使用res的当前索引更新index并将当前数字 + 2 i附加到列表res
  • 索引位置旋转列表 res[]。
  • 完成上述步骤后,打印列表res[]作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the permutation of
// integers from a given range such that
// number of mismatching bits between
// pairs of adjacent elements is 1
vector circularPermutation(int n, int start)
{
     
    // Initialize an arrayList to
    // store the resultant permutation
    vector res = {0};
    vector ret;
     
    // Store the index of rotation
    int index = -1;
     
    // Iterate over the range [0, N - 1]
    for(int k = 0, add = 1 << k; k < n;
            k++, add = 1 << k)
    {
     
        // Traverse all the array elements
        // up to (2 ^ k)-th index in reverse
        for (int i = res.size() - 1;
                 i >= 0; i--)
        {
         
            // If current element is S
            if (res[i] + add == start)
                index = res.size();
                 
            res.push_back(res[i] + add);
        }
    }
     
    // Check if S is zero
    if (start == 0)
        return res;
     
    // Rotate the array by index
    // value to the left
    while (ret.size() < res.size())
    {
        ret.push_back(res[index]);
        index = (index + 1) % res.size();
    }
    return ret;
}
 
// Driver Code
int main()
{
    int N = 2, S = 3;
    vector print = circularPermutation(N, S);
    cout << "[";
    for(int i = 0; i < print.size() - 1; i++ )
    {
        cout << print[i] << ", ";
    }
    cout << print[print.size() - 1] << "]";
     
    return 0;
}
 
// This code is contributed by susmitakundugoaldanga


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the permutation of
    // integers from a given range such that
    // number of mismatching bits between
    // pairs of adjacent elements is 1
    public static List circularPermutation(
        int n, int start)
    {
        // Initialize an arrayList to
        // store the resultant permutation
        List res = new ArrayList<>(List.of(0)),
                      ret = new ArrayList<>();
 
        // Store the index of rotation
        int index = -1;
 
        // Iterate over the range [0, N - 1]
        for (int k = 0, add = 1 << k; k < n;
             k++, add = 1 << k) {
 
            // Traverse all the array elements
            // up to (2 ^ k)-th index in reverse
            for (int i = res.size() - 1;
                 i >= 0; i--) {
 
                // If current element is S
                if (res.get(i) + add == start)
                    index = res.size();
 
                res.add(res.get(i) + add);
            }
        }
 
        // Check if S is zero
        if (start == 0)
            return res;
 
        // Rotate the array by index
        // value to the left
        while (ret.size() < res.size()) {
            ret.add(res.get(index));
            index = (index + 1) % res.size();
        }
 
        return ret;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, S = 3;
 
        System.out.println(
            circularPermutation(N, S));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the permutation of
# integers from a given range such that
# number of mismatching bits between
# pairs of adjacent elements is 1
def circularPermutation(n, start):
   
    # Initialize an arrayList to
    # store the resultant permutation
    res = [0]
    ret = []
 
    # Store the index of rotation
    index, add = -1, 1
 
    # Iterate over the range [0, N - 1]
    for k in range(n):
        add = 1<


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find the permutation of
  // integers from a given range such that
  // number of mismatching bits between
  // pairs of adjacent elements is 1
  public static List circularPermutation(
    int n, int start)
  {
 
    // Initialize an arrayList to
    // store the resultant permutation
    List res = new List(){0};
    List ret = new List();
 
    // Store the index of rotation
    int index = -1;
 
    // Iterate over the range [0, N - 1]
    for (int k = 0, add = 1 << k; k < n;
         k++, add = 1 << k)
    {
 
      // Traverse all the array elements
      // up to (2 ^ k)-th index in reverse
      for (int i = res.Count - 1;
           i >= 0; i--)
      {
 
        // If current element is S
        if (res[i] + add == start)
          index = res.Count;
        res.Add(res[i] + add);
      }
    }
 
    // Check if S is zero
    if (start == 0)
      return res;
 
    // Rotate the array by index
    // value to the left
    while (ret.Count < res.Count)
    {
      ret.Add(res[index]);
      index = (index + 1) % res.Count;
    }
    return ret;
  }
 
  // Driver Code
  static public void Main ()
  {
    int N = 2, S = 3;
    List print = circularPermutation(N, S);
    Console.Write("[");
    for(int i = 0; i < print.Count - 1; i++ )
    {
      Console.Write(print[i] + ", ");
    }
    Console.Write(print[print.Count-1] + "]");
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:
[3, 2, 0, 1]

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