Constructing Palindromic Arrays with First Element as X
Last Updated :
12 Feb, 2024
Given two integers N and X (1 <= X <= N). The task is to find a permutation P of [1,2,3…N] with the first element as X, such that the difference array Diff[], where Diffi = P(i+1) – Pi for all i (1 <= i < N), forms a palindrome. If no such permutation exists, output -1.
Examples:
Input: N = 4, X = 3
Output: P[] = {3, 1, 4, 2}
Explanation: If P[] = {3, 1, 4, 2}, Then:
- First element of P[] is equal to X = 3, which is true.
- Calculating Diff[] for all i (1 <= i < N) gives = {(P2-P1), (P3-P2), (P4-P3)} = {(1-3), (4-1), (2-4)} = {-2, 3, -2}, which is palindrome.
Thus, P[] follows all the conditions, having length N and all elements are from the range [1, N].
Input: N = 3, X = 2
Output: -1
Explanation: It can be verified that no such P[] exists for the given values of N and X.
Approach:
The problem is observation based. The main idea is:
- If N is odd and X is equal to (N + 1)/2, then no such P[] exists.
- Otherwise, a possible permutation P[] will always be there. Which can be obtained by following below sequence:
- First, we have to print the X.
- Then print all numbers from 1 to N except X and (N – X + 1).
- Finally print (N – X + 1).
Step-by-step approach:
- Check Special Case:
- If N is odd and X is equal to (N + 1) / 2, output -1.
- Generate Permutation:
- Calculate Y = N – X + 1.
- Print X.
- Print all numbers from 1 to N except X and Y.
- Print Y.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void Find_permutation( int N, int X)
{
if (N % 2 == 1 && X == (N + 1) / 2) {
cout << -1;
return ;
}
int Y = N - X + 1;
cout << X << " " ;
for ( int i = 1; i <= N; ++i) {
if (i == X || i == Y)
continue ;
cout << i << " " ;
}
cout << Y << " " ;
}
int main()
{
int N = 4, X = 4;
Find_permutation(N, X);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int N = 4 , X = 4 ;
Find_permutation(N, X);
}
public static void Find_permutation( int N, int X)
{
if (N % 2 == 1 && X == (N + 1 ) / 2 ) {
System.out.println(- 1 );
return ;
}
int Y = N - X + 1 ;
System.out.print(X + " " );
for ( int i = 1 ; i <= N; ++i) {
if (i == X || i == Y)
continue ;
System.out.print(i + " " );
}
System.out.print(Y + " " );
}
}
|
Python3
def find_permutation(N, X):
if N % 2 = = 1 and X = = (N + 1 ) / / 2 :
print ( - 1 )
return
Y = N - X + 1
print (X, end = " " )
for i in range ( 1 , N + 1 ):
if i = = X or i = = Y:
continue
print (i, end = " " )
print (Y, end = " " )
if __name__ = = "__main__" :
N = 4
X = 4
find_permutation(N, X)
|
C#
using System;
class GFG
{
static void FindPermutation( int N, int X)
{
if (N % 2 == 1 && X == (N + 1) / 2)
{
Console.WriteLine(-1);
return ;
}
int Y = N - X + 1;
Console.Write(X + " " );
for ( int i = 1; i <= N; ++i)
{
if (i == X || i == Y)
continue ;
Console.Write(i + " " );
}
Console.Write(Y + " " );
}
static void Main()
{
int N = 4, X = 4;
FindPermutation(N, X);
}
}
|
Javascript
function findPermutation(N, X) {
if (N % 2 === 1 && X === Math.floor((N + 1) / 2)) {
console.log(-1);
return ;
}
const Y = N - X + 1;
process.stdout.write(X + " " );
for (let i = 1; i <= N; ++i) {
if (i === X || i === Y)
continue ;
process.stdout.write(i + " " );
}
console.log(Y + " " );
}
function main() {
const N = 4, X = 4;
findPermutation(N, X);
}
main();
|
Time Complexity: O(N)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...