Given two integer N and K, the task is to find the permutation P of first N natural numbers such that there are exactly K elements which satisfies the condition GCD(P[i], i) > 1 for all 1 ≤ i ≤ N.
Input: N = 3, K = 1
Output: 2 1 3
GCD(P, 1) = GCD(2, 1) = 1
GCD(P, 2) = GCD(1, 2) = 1
GCD(P, 3) = GCD(3, 3) = 3
There is exactly 1 element such that GCD(P[i], i) > 1
Input: N = 5, K = 2
Output: 3 1 2 4 5
Approach: Keep the last K elements in their place. The rest of the elements are moved such that ith element is placed in (i + 1)th position and (N – K)th element is kept in position 1 because gcd(x, x + 1) = 1.
Below is the implementation of the above approach:
3 1 2 4 5
- Subarray permutation that satisfies the given condition
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Find the good permutation of first N natural numbers
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find the lexicographically smallest string which satisfies the given condition
- Increasing permutation of first N natural numbers
- Maximum size of sub-array that satisfies the given condition
- Smallest index in the given array that satisfies the given condition
- Partition the digits of an integer such that it satisfies a given condition
- Program to find sum of first n natural numbers
- Find if given number is sum of first n natural numbers
- Find the average of first N natural numbers
- Find sum of N-th group of Natural Numbers
- Find m-th summation of first n natural numbers.
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.