Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
Last Updated :
26 Sep, 2022
Given an integer N. The task is to find the permutation of first N natural numbers such that the absolute difference between any two consecutive numbers > 1. If no such permutation is possible then print -1.
Examples:
Input: N = 5
Output: 5 3 1 4 2
Input: N = 3
Output: -1
Approach: There may be many such arrangements possible but one of the most common and greedy approach is to arrange all odd numbers in decreasing (or increasing) order and after that arrange all even numbers in decreasing (or increasing) order. Note that if N = 3 or N = 2 then there will be no such arrangement possible and if N = 1 then the sequence will consist of a single element i.e. 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void arrange( int N)
{
if (N == 1) {
cout << "1" ;
return ;
}
if (N == 2 || N == 3) {
cout << "-1" ;
return ;
}
int even = -1, odd = -1;
if (N % 2 == 0) {
even = N;
odd = N - 1;
}
else {
odd = N;
even = N - 1;
}
while (odd >= 1) {
cout << odd << " " ;
odd = odd - 2;
}
while (even >= 2) {
cout << even << " " ;
even = even - 2;
}
}
int main()
{
int N = 5;
arrange(N);
return 0;
}
|
Java
class GFG
{
static void arrange( int N)
{
if (N == 1 )
{
System.out.println( "1" );
return ;
}
if (N == 2 || N == 3 )
{
System.out.println( "-1" );
return ;
}
int even = - 1 , odd = - 1 ;
if (N % 2 == 0 )
{
even = N;
odd = N - 1 ;
}
else
{
odd = N;
even = N - 1 ;
}
while (odd >= 1 )
{
System.out.print(odd);
System.out.print( " " );
odd = odd - 2 ;
}
while (even >= 2 )
{
System.out.print(even);
System.out.print( " " );
even = even - 2 ;
}
}
public static void main(String[] args)
{
int N = 5 ;
arrange(N);
}
}
|
Python3
def arrange(N):
if (N = = 1 ) :
print ( "1" )
return
if (N = = 2 or N = = 3 ) :
print ( "-1" )
return
even = - 1
odd = - 1
if (N % 2 = = 0 ):
even = N
odd = N - 1
else :
odd = N
even = N - 1
while (odd > = 1 ):
print (odd, end = " " )
odd = odd - 2
while (even > = 2 ):
print (even, end = " " )
even = even - 2
if __name__ = = "__main__" :
N = 5
arrange(N)
|
C#
using System;
class GFG
{
static void arrange( int N)
{
if (N == 1)
{
Console.WriteLine( "1" );
return ;
}
if (N == 2 || N == 3)
{
Console.WriteLine( "-1" );
return ;
}
int even = -1, odd = -1;
if (N % 2 == 0)
{
even = N;
odd = N - 1;
}
else
{
odd = N;
even = N - 1;
}
while (odd >= 1)
{
Console.Write(odd);
Console.Write( " " );
odd = odd - 2;
}
while (even >= 2)
{
Console.Write(even);
Console.Write( " " );
even = even - 2;
}
}
public static void Main()
{
int N = 5;
arrange(N);
}
}
|
PHP
<?php
function arrange( $N )
{
if ( $N == 1)
{
echo "1" ;
return ;
}
if ( $N == 2 || $N == 3)
{
echo "-1" ;
return ;
}
$even = -1 ;
$odd = -1;
if ( $N % 2 == 0)
{
$even = $N ;
$odd = $N - 1;
}
else
{
$odd = $N ;
$even = $N - 1;
}
while ( $odd >= 1)
{
echo $odd , " " ;
$odd = $odd - 2;
}
while ( $even >= 2)
{
echo $even , " " ;
$even = $even - 2;
}
}
$N = 5;
arrange( $N );
?>
|
Javascript
<script>
function arrange(N)
{
if (N == 1)
{
document.write( "1" );
return ;
}
if (N == 2 || N == 3)
{
document.write( "-1" );
return ;
}
var even = -1, odd = -1;
if (N % 2 == 0)
{
even = N;
odd = N - 1;
}
else
{
odd = N;
even = N - 1;
}
while (odd >= 1)
{
document.write(odd);
document.write( " " );
odd = odd - 2;
}
while (even >= 2)
{
document.write(even);
document.write( " " );
even = even - 2;
}
}
var N = 5;
arrange(N);
</script>
|
Time complexity: O(N), where N is the given integer.
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...