📌  相关文章
📜  生成一个长度为 N 的排列,使得相邻元素之间的绝对差异出现在 [2, 4] 范围内

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

生成一个长度为 N 的排列,使得相邻元素之间的绝对差异出现在 [2, 4] 范围内

给定一个正整数N ,任务是构造前N个自然数的排列 这样相邻元素之间的绝对差值为234 。如果无法构造这样的排列,则打印“-1”

例子:

方法:给定的问题可以通过将连续的偶数和奇数元素组合在一起来构造排列来解决。请按照以下步骤解决问题:

  • 如果N的值小于4 ,则打印-1 ,因为不可能根据N小于4的给定条件构造排列。
  • 初始化一个变量,比如iN ,然后执行以下步骤:
    • 如果i的值是偶数,则将i的值减1
    • 迭代直到i的值至少为1并打印i的值并将i的值递减2
    • 打印42并将i的值更新为6
    • [i, N]范围内迭代并打印i的值并将i的值增加2

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the permutation of
// size N with absolute difference of
// adjacent elements in range [2, 4]
void getPermutation(int N)
{
    // If N is less than 4
    if (N <= 3) {
        cout << -1;
        return;
    }
 
    int i = N;
 
    // Check if N is even
    if (N % 2 == 0)
        i--;
 
    // Traverse through odd integers
    while (i >= 1) {
        cout << i << " ";
        i -= 2;
    }
 
    cout << 4 << " " << 2 << " ";
 
    // Update the value of i
    i = 6;
 
    // Traverse through even integers
    while (i <= N) {
        cout << i << " ";
        i += 2;
    }
}
 
// Driver Code
int main()
{
    int N = 9;
    getPermutation(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to print the permutation of
// size N with absolute difference of
// adjacent elements in range [2, 4]
static void getPermutation(int N)
{
     
    // If N is less than 4
    if (N <= 3)
    {
        System.out.print(-1);
        return;
    }
 
    int i = N;
 
    // Check if N is even
    if (N % 2 == 0)
        i--;
 
    // Traverse through odd integers
    while (i >= 1)
    {
        System.out.print(i + " ");
        i -= 2;
    }
 
    System.out.print(4 + " " + 2 +" ");
 
    // Update the value of i
    i = 6;
 
    // Traverse through even integers
    while (i <= N)
    {
        System.out.print(i + " ");
        i += 2;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 9;
     
    getPermutation(N);
}   
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to print permutation of
# size N with absolute difference of
# adjacent elements in range [2, 4]
def getPermutation(N):
   
    # If N is less than 4
    if (N <= 3):
        print(-1)
        return
 
    i = N
 
    # Check if N is even
    if (N % 2 == 0):
        i -= 1
 
    # Traverse through odd integers
    while (i >= 1):
        print(i, end = " ")
        i -= 2
 
    print(4, 2, end = " ")
 
    # Update the value of i
    i = 6
 
    # Traverse through even integers
    while (i <= N):
        print( i, end = " ")
        i += 2
 
# Driver Code
if __name__ == '__main__':
    N = 9
    getPermutation(N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to print the permutation of
// size N with absolute difference of
// adjacent elements in range [2, 4]
static void getPermutation(int N)
{
     
    // If N is less than 4
    if (N <= 3)
    {
         
        Console.Write(-1);
        return;
    }
 
    int i = N;
 
    // Check if N is even
    if (N % 2 == 0)
        i--;
 
    // Traverse through odd integers
    while (i >= 1)
    {
        Console.Write(i + " ");
        i -= 2;
    }
 
    Console.Write(4 + " " + 2 +" ");
 
    // Update the value of i
    i = 6;
 
    // Traverse through even integers
    while (i <= N)
    {
        Console.Write(i +" ");
        i += 2;
    }
}
 
// Driver Code
public static void Main()
{
    int N = 9;
     
    getPermutation(N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
9 7 5 3 1 4 2 6 8

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