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.
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
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 atmost 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:
1 2 3 4
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest string which differs from given strings at exactly K indices
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Number of valid indices in the permutation of first N natural numbers
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find an integer X which is divisor of all except exactly one element in an array
- Smallest index in given range of indices which is not equal to X
- Count number of integers less than or equal to N which has exactly 9 divisors
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Check if there exists a number with X factors out of which exactly K are prime
- Count of subarrays which contains a given number exactly K times
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Count the pairs in an array such that the difference between them and their indices is equal
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.