📌  相关文章
📜  排列前N个自然数,以使所有相邻元素之间的绝对差> 1

📅  最后修改于: 2021-06-25 15:04:50             🧑  作者: Mango

给定整数N。任务是找到前N个自然数的置换,以使任意两个连续数之间的绝对差> 1 。如果不可能进行这种排列,则打印-1

例子:

方法:可能有许多这样的排列方式,但是最常见和贪婪的方法之一是以降序(或递增)顺序排列所有奇数,然后以递减(或递增)顺序排列所有偶数。请注意,如果N = 3N = 2,那么将不可能有这种排列方式;如果N = 1,则序列将由单个元素组成,即1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the required permutation
void arrange(int N)
{
 
    if (N == 1) {
        cout << "1";
        return;
    }
 
    // No permutation is possible
    // satisfying the given condition
    if (N == 2 || N == 3) {
        cout << "-1";
        return;
    }
 
    // Maximum even and odd elements < N
    int even = -1, odd = -1;
    if (N % 2 == 0) {
        even = N;
        odd = N - 1;
    }
    else {
        odd = N;
        even = N - 1;
    }
 
    // Print all odd elements in decreasing order
    while (odd >= 1) {
        cout << odd << " ";
 
        // Next element must be odd
        odd = odd - 2;
    }
 
    // Print all even elements in decreasing order
    while (even >= 2) {
        cout << even << " ";
 
        // Next element must be even
        even = even - 2;
    }
}
 
// Driver code
int main()
{
    int N = 5;
    arrange(N);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to print the required
// permutation
static void arrange(int N)
{
    if (N == 1)
    {
        System.out.println("1");
        return;
    }
 
    // No permutation is possible
    // satisfying the given condition
    if (N == 2 || N == 3)
    {
        System.out.println("-1");
        return;
    }
 
    // Maximum even and odd elements < N
    int even = -1, odd = -1;
    if (N % 2 == 0)
    {
        even = N;
        odd = N - 1;
    }
    else
    {
        odd = N;
        even = N - 1;
    }
 
    // Print all odd elements in
    // decreasing order
    while (odd >= 1)
    {
        System.out.print(odd);
        System.out.print(" ");
     
        // Next element must be odd
        odd = odd - 2;
    }
 
    // Print all even elements in
    // decreasing order
    while (even >= 2)
    {
        System.out.print(even);
        System.out.print(" ");
 
        // Next element must be even
        even = even - 2;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
    arrange(N);
}
}
 
// This code is contributed
// by Akanksha Rai


Python3
# Python3 implementation of the approach
 
# Function to print the required permutation
def arrange(N):
 
    if (N == 1) :
        print("1")
        return
 
    # No permutation is possible
    # satisfying the given condition
    if (N == 2 or N == 3) :
        print("-1")
        return
 
    # Maximum even and odd elements < N
    even = -1
    odd = -1
    if (N % 2 == 0):
        even = N
        odd = N - 1
    else :
        odd = N
        even = N - 1
 
    # Print all odd elements in
    # decreasing order
    while (odd >= 1):
        print(odd, end = " ")
 
        # Next element must be odd
        odd = odd - 2
 
    # Print all even elements in
    # decreasing order
    while (even >= 2):
        print(even, end = " ")
 
        # Next element must be even
        even = even - 2
 
# Driver code
if __name__ == "__main__":
 
    N = 5
    arrange(N)
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print the required
// permutation
static void arrange(int N)
{
    if (N == 1)
    {
        Console.WriteLine("1");
        return;
    }
 
    // No permutation is possible
    // satisfying the given condition
    if (N == 2 || N == 3)
    {
        Console.WriteLine("-1");
        return;
    }
 
    // Maximum even and odd elements < N
    int even = -1, odd = -1;
    if (N % 2 == 0)
    {
        even = N;
        odd = N - 1;
    }
    else
    {
        odd = N;
        even = N - 1;
    }
 
    // Print all odd elements in
    // decreasing order
    while (odd >= 1)
    {
        Console.Write(odd);
        Console.Write(" ");
     
        // Next element must be odd
        odd = odd - 2;
    }
 
    // Print all even elements in
    // decreasing order
    while (even >= 2)
    {
        Console.Write(even);
        Console.Write(" ");
 
        // Next element must be even
        even = even - 2;
    }
}
 
// Driver code
public static void Main()
{
    int N = 5;
    arrange(N);
}
}
 
// This code is contributed
// by Shivi_Aggarwal


PHP
= 1)
    {
        echo $odd, " ";
 
        // Next element must be odd
        $odd = $odd - 2;
    }
 
    // Print all even elements in
    // decreasing order
    while ($even >= 2)
    {
        echo $even, " ";
 
        // Next element must be even
        $even = $even - 2;
    }
}
 
// Driver code
$N = 5;
arrange($N);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
5 3 1 4 2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。