- 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
Minimum insertions to make a Co-prime array in C++
In this section we will see another interesting problem. Suppose we have an array of N elements. We have to find minimum number of intersection points to make this array as co-prime array. In the co-prime array gcd of every two consecutive elements is 1. We have to print the array also.
Suppose we have elements like {5, 10, 20}. This is not co-prime array. Now by inserting 1 between 5, 10 and 10, 20, it will be co-prime array. So the array will be like {5, 1, 10, 1, 20}
Algorithm
makeCoPrime(arr, n): begin count := 0 for i in range 1 to n, do if gcd of arr[i] and arr[i – 1] is not 1, then increase count by 1 done display count value display the first element of arr for i in range 1 to n, do if gcd of arr[i] and arr[i – 1] is not 1, then display 1 display element arr[i] done end
Example
#include <iostream> #include <algorithm> using namespace std; int makeCoPrime(int arr[], int n){ int count = 0; for(int i = 1; i<n; i++){ if(__gcd(arr[i], arr[i - 1]) != i){ count++; } } cout << "Number of intersection points: " << count << endl; cout << arr[0] << " "; for(int i = 1; i<n; i++){ if(__gcd(arr[i], arr[i - 1]) != i){ cout << 1 << " "; } cout << arr[i] << " "; } } int main() { int A[] = {2, 7, 28}; int n = sizeof(A)/sizeof(A[0]); makeCoPrime(A, n); }
Output
Number of intersection points: 1 2 7 1 28
- Related Articles
- Minimum Increment to Make Array Unique in C++
- Minimum removals to make array sum even in C++
- Minimum removals to make array sum odd in C++
- C Program to Minimum and Maximum prime numbers in an array
- Program to find minimum insertions to balance a parentheses string using Python
- Minimum operations to make XOR of array zero in C++
- Minimum removals from array to make GCD Greater in C++
- Minimum operation to make all elements equal in array in C++
- Minimum operations to make GCD of array a multiple of k in C++
- Minimum number of deletions and insertions to transform one string into another using C++.
- Find minimum operations needed to make an Array beautiful in C++
- What are prime numbers and co-prime numbers?
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations required to make all the array elements equal in C++
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++

Advertisements