Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
Last Updated :
22 Jun, 2022
Given two integers N and K, the task is to find a permutation of integers from the range [1, N] such that the number of indices (1-based indexing) where gcd(p[i], i) > 1 is exactly K. Print -1 if such permutation is not possible.
Examples:
Input: N = 4, K = 3
Output: 1 2 3 4
gcd(1, 1) = 1
gcd(2, 2) = 2
gcd(3, 3) = 3
gcd(4, 4) = 4
Therefore, there are exactly 3 indices where gcd(p[i], i) > 1
Input: N = 1, K = 1
Output: -1
Approach: A couple of observations can be made here:
- gcd(i, i + 1) = 1
- gcd(1, i) = 1
- gcd(i, i) = i
Since gcd of 1 with any other number is always going to be one, K can almost be N – 1. Consider the permutation where p[i] = i, The number of indices where gcd(p[i], i) > 1 will be N – 1. Notice that swapping 2 continuous elements (excluding 1) will reduce the count of such indices by exactly 2. And swapping with 1 will reduce the count by exactly 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPermutation( int n, int k)
{
if (k >= n || (n % 2 == 0 && k == 0)) {
cout << -1;
return ;
}
int per[n + 1], i;
for (i = 1; i <= n; i++)
per[i] = i;
int cnt = n - 1;
i = 2;
while (i < n) {
if (cnt - 1 > k) {
swap(per[i], per[i + 1]);
cnt -= 2;
}
else if (cnt - 1 == k) {
swap(per[1], per[i]);
cnt--;
}
else
break ;
i += 2;
}
for (i = 1; i <= n; i++)
cout << per[i] << " " ;
}
int main()
{
int n = 4, k = 3;
printPermutation(n, k);
return 0;
}
|
Java
class GFG
{
static void printPermutation( int n, int k)
{
if (k >= n || (n % 2 == 0 && k == 0 ))
{
System.out.print(- 1 );
return ;
}
int per[] = new int [n + 1 ], i;
for (i = 1 ; i <= n; i++)
{
per[i] = i;
}
int cnt = n - 1 ;
i = 2 ;
while (i < n)
{
if (cnt - 1 > k)
{
swap(per, i, i + 1 );
cnt -= 2 ;
}
else if (cnt - 1 == k)
{
swap(per, 1 , i);
cnt--;
}
else
{
break ;
}
i += 2 ;
}
for (i = 1 ; i <= n; i++)
{
System.out.print(per[i] + " " );
}
}
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args)
{
int n = 4 , k = 3 ;
printPermutation(n, k);
}
}
|
Python3
def printPermutation(n, k):
if (k > = n or (n % 2 = = 0 and
k = = 0 )):
print ( - 1 );
return ;
per = [ 0 ] * (n + 1 );
for i in range ( 1 , n + 1 ):
per[i] = i;
cnt = n - 1 ;
i = 2 ;
while (i < n):
if (cnt - 1 > k):
t = per[i];
per[i] = per[i + 1 ];
per[i + 1 ] = t;
cnt - = 2 ;
elif (cnt - 1 = = k):
t = per[ 1 ];
per[ 1 ] = per[i];
per[i] = t;
cnt - = 1 ;
else :
break ;
i + = 2 ;
for i in range ( 1 , n + 1 ):
print (per[i], end = " " );
n = 4 ;
k = 3 ;
printPermutation(n, k);
|
C#
using System;
class GFG
{
static void printPermutation( int n, int k)
{
if (k >= n || (n % 2 == 0 && k == 0))
{
Console.Write(-1);
return ;
}
int []per = new int [n + 1] ;
int i ;
for (i = 1; i <= n; i++)
{
per[i] = i;
}
int cnt = n - 1;
i = 2;
while (i < n)
{
if (cnt - 1 > k)
{
swap(per, i, i + 1);
cnt -= 2;
}
else if (cnt - 1 == k)
{
swap(per, 1, i);
cnt--;
}
else
{
break ;
}
i += 2;
}
for (i = 1; i <= n; i++)
{
Console.Write(per[i] + " " );
}
}
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void Main()
{
int n = 4, k = 3;
printPermutation(n, k);
}
}
|
PHP
<?php
function printPermutation( $n , $k )
{
if ( $k >= $n || ( $n % 2 == 0 &&
$k == 0))
{
echo -1;
return ;
}
$per [ $n + 1] = array ();
$i ;
for ( $i = 1; $i <= $n ; $i ++)
$per [ $i ] = $i ;
$cnt = $n - 1;
$i = 2;
while ( $i < $n )
{
if ( $cnt - 1 > $k )
{
list( $per [ $i ],
$per [ $i + 1]) = array ( $per [ $i + 1],
$per [ $i ]);
$cnt -= 2;
}
else if ( $cnt - 1 == $k )
{
list( $per [1],
$per [ $i ]) = array ( $per [ $i ],
$per [1]);
$cnt --;
}
else
break ;
$i += 2;
}
for ( $i = 1; $i <= $n ; $i ++)
echo $per [ $i ], " " ;
}
$n = 4;
$k = 3;
printPermutation( $n , $k );
?>
|
Javascript
<script>
function printPermutation(n, k)
{
if (k >= n || (n % 2 == 0 && k == 0))
{
document.write(-1);
return ;
}
let per = new Array(n + 1);
per.fill(0);
let i ;
for (i = 1; i <= n; i++)
{
per[i] = i;
}
let cnt = n - 1;
i = 2;
while (i < n)
{
if (cnt - 1 > k)
{
swap(per, i, i + 1);
cnt -= 2;
}
else if (cnt - 1 == k)
{
swap(per, 1, i);
cnt--;
}
else
{
break ;
}
i += 2;
}
for (i = 1; i <= n; i++)
{
document.write(per[i] + " " );
}
}
function swap(arr, i, j)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
let n = 4, k = 3;
printPermutation(n, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...