📌  相关文章
📜  生成 1 到 N 的排列,没有相邻元素差为 1

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

生成 1 到 N 的排列,没有相邻元素差为 1

给定一个整数N ,任务是构造一个从1 到 N的排列,其中没有相邻元素的差为1。如果没有这样的排列,则打印 -1。

例子:

方法:可以根据以下思路解决问题:

按照下面提到的步骤来实施上述方法:

  • 如果 N 小于或等于 3,则不可能有这样的排列。
  • 如果 N 是偶数,首先打印从2 到 N的所有偶数,然后从1 到 N-1 的所有奇数打印所有奇数。
  • 如果N是奇数,则打印从2 到 N - 1的所有偶数,然后打印从 1 到 N 的所有奇数

下面是上述方法的实现:

C++
// C++ code for the above approach
 
#include 
using namespace std;
 
// Function to find the permutation
// which satisfies the given condition
vector permutation(int n)
{
    vector ans;
    if (n <= 3) {
        ans.push_back(-1);
    }
 
    // If n is even
    if (n % 2 == 0) {
        for (int i = 2; i <= n; i += 2) {
            ans.push_back(i);
        }
        for (int i = 1; i < n; i += 2) {
            ans.push_back(i);
        }
    }
 
    // If n is odd
    else {
        for (int i = 2; i <= n - 1; i += 2) {
            ans.push_back(i);
        }
        for (int j = 1; j <= n; j += 2) {
            ans.push_back(j);
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    int N = 5;
    vector ans = permutation(N);
    for (int x : ans)
        cout << x << " ";
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG {
 
  // Function to find the permutation
  // which satisfies the given condition
  static List permutation(int n)
  {
    List ans = new ArrayList<>();
    if (n <= 3) {
      ans.add(-1);
    }
 
    // If n is even
    if (n % 2 == 0) {
      for (int i = 2; i <= n; i += 2) {
        ans.add(i);
      }
      for (int i = 1; i < n; i += 2) {
        ans.add(i);
      }
    }
 
    // If n is odd
    else {
      for (int i = 2; i <= n - 1; i += 2) {
        ans.add(i);
      }
      for (int j = 1; j <= n; j += 2) {
        ans.add(j);
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 5;
    List ans = permutation(N);
    for (Integer x : ans)
      System.out.print(x + " ");
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python program to generate permutation of 1 to n
# with no adjacent element difference as 1
def permutation(n):
 
    # for storing the resultant permutations
    ans = []
 
    if n <= 3:
        ans.append(-1)
 
    # if n is even
    if n % 2 == 0:
        i = 0
        while i <= n:
            ans.append(i)
            i += 2
        i = 1
        while i < n:
            ans.append(i)
            i += 2
 
    # if n is odd
    else:
        i = 2
        while i <= n-1:
            ans.append(i)
            i += 2
        j = 1
        while j <= n:
            ans.append(j)
            j += 2
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    n = 5
    ans = permutation(n)
    for i in ans:
        print(i, end=" ")
 
        # This code is contributed by Amnindersingh1414.


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to find the permutation
  // which satisfies the given condition
  static List permutation(int n) {
    List ans = new List();
    if (n <= 3) {
      ans.Add(-1);
    }
 
    // If n is even
    if (n % 2 == 0) {
      for (int i = 2; i <= n; i += 2) {
        ans.Add(i);
      }
      for (int i = 1; i < n; i += 2) {
        ans.Add(i);
      }
    }
 
    // If n is odd
    else {
      for (int i = 2; i <= n - 1; i += 2) {
        ans.Add(i);
      }
      for (int j = 1; j <= n; j += 2) {
        ans.Add(j);
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    int N = 5;
    List ans = permutation(N);
    foreach (int x in ans)
      Console.Write(x + " ");
  }
}
 
// This code is contributed by gauravrajput1


Javascript



输出
2 4 1 3 5 

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