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., 9Input: 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++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print valid permutation of N
void findPerm(int n)
{
// Initialize the pointers
int i = 1, j = n;
// Declare an array of size N
int arr[n];
// Constructing the array
for (int x = 0; x < n; x++) {
if (x & 1)
arr[x] = j--;
else
arr[x] = i++;
}
// Printing the array
for (int x = (n - 1); x >= 0; x--) {
cout << arr[x] << " ";
}
}
// Driver Code
int main()
{
int N = 10;
// Function Call
findPerm(N);
return 0;
}
// Java code to implement the above approach
public class GFG {
// Function to print valid permutation of N
static void findPerm(int n)
{
// Initialize the pointers
int i = 1, j = n;
// Declare an array of size N
int arr[] = new int[n];
// Constructing the array
for (int x = 0; x < n; x++) {
if ((x & 1) == 1)
arr[x] = j--;
else
arr[x] = i++;
}
// Printing the array
for (int x = (n - 1); x >= 0; x--) {
System.out.print(arr[x]+" ");
}
}
// Driver Code
public static void main (String[] args)
{
int N = 10;
// Function Call
findPerm(N);
}
}
// This code is contributed by AnkThon
# python3 code to implement the above approach
# Function to print valid permutation of N
def findPerm(n):
# Initialize the pointers
i, j = 1, n
# Declare an array of size N
arr = [0 for _ in range(n)]
# Constructing the array
for x in range(0, n):
if (x & 1):
arr[x] = j
j -= 1
else:
arr[x] = i
i += 1
# Printing the array
for x in range(n-1, -1, -1):
print(arr[x], end=" ")
# Driver Code
if __name__ == "__main__":
N = 10
# Function Call
findPerm(N)
# This code is contributed by rakeshsahni
// C# code to implement the above approach
using System;
public class GFG {
// Function to print valid permutation of N
static void findPerm(int n)
{
// Initialize the pointers
int i = 1, j = n;
// Declare an array of size N
int[] arr = new int[n];
// Constructing the array
for (int x = 0; x < n; x++) {
if ((x & 1) == 1)
arr[x] = j--;
else
arr[x] = i++;
}
// Printing the array
for (int x = (n - 1); x >= 0; x--) {
Console.Write(arr[x] + " ");
}
}
// Driver Code
public static void Main(string[] args)
{
int N = 10;
// Function Call
findPerm(N);
}
}
// This code is contributed by phasing17
<script>
// Javascript code to implement the above approach
// Function to print valid permutation of N
function findPerm(n)
{
// Initialize the pointers
let i = 1, j = n;
// Declare an array of size N
let arr=new Array(n);
// Constructing the array
for (let x = 0; x < n; x++) {
if (x & 1)
arr[x] = j--;
else
arr[x] = i++;
}
// Printing the array
for (let x = (n - 1); x >= 0; x--) {
document.write(arr[x] + " ");
}
}
// Driver Code
let N = 10;
// Function Call
findPerm(N);
// This code is contributed by satwik4409.
</script>
Output
6 5 7 4 8 3 9 2 10 1
Time Complexity: O(N)
Auxiliary Space: O(N)