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 lexicographically smallest string which satisfies the given condition
- Smallest index in the given array that satisfies the given condition
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Maximum size of sub-array that satisfies the given condition
- Partition the digits of an integer such that it satisfies a given condition
- 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 permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Find the K-th Permutation Sequence of first N natural numbers
- Increasing permutation of first N natural numbers
- Number of valid indices in the permutation of first N natural numbers
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Find an integer in the given range that satisfies the given conditions
- Find if given number is sum of first n natural numbers
- Sort permutation of N natural numbers using triple cyclic right swaps
- Generate an array B from the given array A which satisfies the given conditions
- Minimum number of given operations required to convert a permutation into an identity permutation
- Find m-th summation of first n natural numbers.
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.