Permutation of first N elements with absolute adjacent difference in increasing order
Last Updated :
25 Jul, 2022
Given a positive integer N, the task is to construct a permutation from 1 to N such that the absolute difference of elements is in strictly increasing order.
Note: N cannot be 0 or 1.
Examples:
Input: N = 10
Output: 6 5 7 4 8 3 9 2 10 1
Explanation: abs(6 – 5) i.e., 1 < abs(5 – 7) i.e., 2 < abs(7 – 4) i.e., 3 …. < abs(2 – 10) i.e., 8 < abs(10 – 1) i.e., 9
Input: 3
Output: 2 3 1
Explanation: abs(2 – 3) = 1 and abs(3 – 1) = 2, 1 < 2 hence it is in strictly increasing order.
Approach: The problem can be solved based on the following observation:
Observation:
Let’s say, you have the i =1 and j = N, the largest absolute difference made is by subtracting 1 and N = (N – 1)
Next Time, i increment by 1, i = 2 and j remains same i.e., N, So, the absolute difference is = (N – 2).
Next Time, i remains same i.e., 2 and j decrement by 1, j = N-1, So, the absolute difference is = (N – 1 – 2) = (N – 3).
Next Time, i increment by 1, i = 3 and j remains same i.e., N-1, So, the absolute difference is = (N – 1 – 3) = (N – 4).
Next Time, i remains same i.e., 3 and j decrement by 1, j = N-2, So, the absolute difference is = (N – 2 – 3) = (N – 5)……
Now, this way the series go, and at last two condition possible,
- When i = j + 1, [If N is odd], absolute difference = 1
- Or, j = i + 1, [If N is even], absolute difference = 1
So, this way the series become for given N, series = (N – 1), (N – 2), (N – 3), …. 3, 2, 1.
Follow the below steps to solve the problem:
- Initialize a pointer i = 1 and j = N.
- Declare an array of size N.
- Run a loop (using iterator x) from 0 to N – 1.
- If x is even then set, arr[x] = i and increment i by 1.
- Else then set, arr[x] = j and decrement j by 1.
- After executing the loop, print the array in reverse order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPerm( int n)
{
int i = 1, j = n;
int arr[n];
for ( int x = 0; x < n; x++) {
if (x & 1)
arr[x] = j--;
else
arr[x] = i++;
}
for ( int x = (n - 1); x >= 0; x--) {
cout << arr[x] << " " ;
}
}
int main()
{
int N = 10;
findPerm(N);
return 0;
}
|
Java
public class GFG {
static void findPerm( int n)
{
int i = 1 , j = n;
int arr[] = new int [n];
for ( int x = 0 ; x < n; x++) {
if ((x & 1 ) == 1 )
arr[x] = j--;
else
arr[x] = i++;
}
for ( int x = (n - 1 ); x >= 0 ; x--) {
System.out.print(arr[x]+ " " );
}
}
public static void main (String[] args)
{
int N = 10 ;
findPerm(N);
}
}
|
Python3
def findPerm(n):
i, j = 1 , n
arr = [ 0 for _ in range (n)]
for x in range ( 0 , n):
if (x & 1 ):
arr[x] = j
j - = 1
else :
arr[x] = i
i + = 1
for x in range (n - 1 , - 1 , - 1 ):
print (arr[x], end = " " )
if __name__ = = "__main__" :
N = 10
findPerm(N)
|
C#
using System;
public class GFG {
static void findPerm( int n)
{
int i = 1, j = n;
int [] arr = new int [n];
for ( int x = 0; x < n; x++) {
if ((x & 1) == 1)
arr[x] = j--;
else
arr[x] = i++;
}
for ( int x = (n - 1); x >= 0; x--) {
Console.Write(arr[x] + " " );
}
}
public static void Main( string [] args)
{
int N = 10;
findPerm(N);
}
}
|
Javascript
<script>
function findPerm(n)
{
let i = 1, j = n;
let arr= new Array(n);
for (let x = 0; x < n; x++) {
if (x & 1)
arr[x] = j--;
else
arr[x] = i++;
}
for (let x = (n - 1); x >= 0; x--) {
document.write(arr[x] + " " );
}
}
let N = 10;
findPerm(N);
</script>
|
Output
6 5 7 4 8 3 9 2 10 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...