Generate Permutation such that GCD of all elements multiplied with position is not 1
Last Updated :
09 May, 2022
Given an integer N and the task is to generate a permutation of the numbers in range [1, N] such that:
- The GCD of all the elements multiplied with their position (not index) is greater than 1
- And if it is not possible return -1.
- If there are multiple possible permutations, print any one of them.
Examples:
Input: N = 8
Output: 2 1 4 3 6 5 8 7
Explanation: The elements multiplied by their positions will be
{2*1, 1*2, 4*3, 3*4, 6*5, 5*6, 8*7, 7*8} = {2, 2, 12, 12, 30, 30, 56, 56}.
The GCD of all these numbers = 2 which is greater than 1.
Input: N = 9
Output: -1
Explanation: No permutation possible, hence return -1
Approach: The idea to solve the problem is as follows:
Try to make the product of position and the number even, then in that situation GCD will be at least 2.
If there are odd number of elements then it is not possible, because odd elements are one more than possible even positions.
Follow the below steps to solve the problem:
- Store all the even numbers in one vector and all the odd numbers in another vector.
- While generating the permutation:
- Push the even number at even index(i.e odd position because the indexing is 0 based) and
- Odd number at odd index(i.e. even position).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > arrangePermutation( int & N)
{
vector< int > odd, even, res;
if (N & 1) {
for ( int i = 1; i <= N; i++) {
res.push_back(-1);
}
return res;
}
for ( int i = 1; i <= N; i++) {
if (i & 1) {
odd.push_back(i);
}
else {
even.push_back(i);
}
}
int k = 0, j = 0;
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
res.push_back(even[k++]);
}
else {
res.push_back(odd[j++]);
}
}
return res;
}
void printResult(vector< int >& res)
{
for ( auto x : res) {
cout << x << " " ;
}
cout << endl;
}
int main()
{
int N = 8;
vector< int > res = arrangePermutation(N);
printResult(res);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int [] arrangePermutation( int N)
{
ArrayList<Integer> odd = new ArrayList<>();
ArrayList<Integer> even = new ArrayList<>();
int res[] = new int [N];
if (N % 2 == 1 ) {
for ( int i = 0 ; i < N; i++) {
res[i] = - 1 ;
}
return res;
}
for ( int i = 1 ; i <= N; i++) {
if (i % 2 == 1 ) {
odd.add(i);
}
else {
even.add(i);
}
}
int k = 0 , j = 0 ;
for ( int i = 0 ; i < N; i++) {
if (i % 2 == 0 ) {
res[i] = even.get(k++);
}
else {
res[i] = odd.get(j++);
}
}
return res;
}
public static void printResult( int res[])
{
for ( int x : res) {
System.out.print(x + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int N = 8 ;
int res[] = new int [N];
res = arrangePermutation(N);
printResult(res);
}
}
|
Python3
def arrangePermutation(N):
odd = []
even = []
res = []
if N & 1 :
for i in range ( 1 , N + 1 ):
res.append( - 1 )
return res
for i in range ( 1 , N + 1 ):
if i & 1 :
odd.append(i)
else :
even.append(i)
k = 0
j = 0
for i in range (N):
if i % 2 = = 0 :
res.append(even[k])
k + = 1
else :
res.append(odd[j])
j + = 1
return res
def printResult(res):
for x in res:
print (x, end = " " )
print ()
N = 8
res = arrangePermutation(N)
printResult(res)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int [] arrangePermutation( int N)
{
List< int > odd = new List< int >();
List< int > even = new List< int >();
int [] res = new int [N];
if (N % 2 == 1) {
for ( int i = 0; i < N; i++) {
res[i] = -1;
}
return res;
}
for ( int i = 1; i <= N; i++) {
if (i % 2 == 1) {
odd.Add(i);
}
else {
even.Add(i);
}
}
int k = 0, j = 0;
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
res[i] = even[k++];
}
else {
res[i] = odd[j++];
}
}
return res;
}
public static void printResult( int [] res)
{
foreach ( int x in res) {
Console.Write(x + " " );
}
Console.WriteLine();
}
public static void Main()
{
int N = 8;
int [] res = new int [N];
res = arrangePermutation(N);
printResult(res);
}
}
|
Javascript
<script>
function arrangePermutation(N) {
let odd = [], even = [], res = [];
if (N & 1) {
for (let i = 1; i <= N; i++) {
res.push(-1);
}
return res;
}
for (let i = 1; i <= N; i++) {
if (i & 1) {
odd.push(i);
}
else {
even.push(i);
}
}
let k = 0, j = 0;
for (let i = 0; i < N; i++) {
if (i % 2 == 0) {
res.push(even[k++]);
}
else {
res.push(odd[j++]);
}
}
return res;
}
function printResult(res) {
for (let x of res) {
document.write(x + " " );
}
document.write( "<br>" )
}
let N = 8;
let res = arrangePermutation(N);
printResult(res);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...