- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++

Suppose we have two integers N and K. We have to find a permutation of integers from the range [1 to N] such that the number of indices (1 – base indexing) where gcd(P[i], i) > 1 is exactly K. So if N = 4 and K = 3, then output will be [1, 2, 3, 4], as gcd(1, 1) = 1, gcd(2, 2) = 2, gcd(3, 3) = 3, gcd(4, 4) = 4

If we observe it carefully, we can find that gcd(i, i+1) = 1, gcd(1, i) = 1 and gcd(i, i) = i. As the GCD of any number and 1 is always 1, K can almost be N – 1. Consider the permutation where P[i] = i. Number of indices where gcd(P[i], i) > 1, will be N – 1. If we swap two consecutive elements excluding 1, will reduce the count of such indices by exactly 2, and swapping with 1 will reduce the count by exactly 1.

## Example

#include<iostream> using namespace std; void findPermutation(int n, int k) { if (k >= n || (n % 2 == 0 && k == 0)) { cout << -1; return; } int P[n + 1]; for (int i = 1; i <= n; i++) P[i] = i; int count = n - 1; for (int i = 2; i < n; i+=2) { if (count - 1 > k) { swap(P[i], P[i + 1]); count -= 2; } else if (count - 1 == k) { swap(P[1], P[i]); count--; } else break; } for (int i = 1; i <= n; i++) cout << P[i] << " "; } int main() { int n = 5, k = 3; cout << "Permutation is: "; findPermutation(n, k); }

## Output

Permutation is: 2 1 3 4 5

- Related Articles
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ≥ 1}
- Find the Number of permutation with K inversions using C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Construct a Turing machine for L = {aibjck | i< j< k; i ≥ 1}
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Rearrange an array such that arr[i] = i in C++

Advertisements