- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Convert the array such that the GCD of the array becomes 1 in C++

In this tutorial, we will be discussing a program to convert the array such that the GCD of the array becomes one.

For this we will be provided with an array and a positive integer k. Our task is to convert the array elements such that the GCD of elements is 1 while only dividing the array elements by k any number of times until the element is less than k.

## Example

#include <bits/stdc++.h> using namespace std; //calculating the GCD of array int calculate_gcd(int* arr, int n){ int gcd = arr[0]; for (int i = 1; i < n; i++) gcd = __gcd(arr[i], gcd); return gcd; } //checking if the operation is possible bool convertGcd(int* arr, int n, int k){ int gcd = calculate_gcd(arr, n); int max_prime = 1; for (int i = 2; i <= sqrt(gcd); i++) { while (gcd % i == 0) { gcd /= i; max_prime = max(max_prime, i); } } max_prime = max(max_prime, gcd); return (max_prime <= k); } int main(){ int arr[] = { 10, 15, 30 }; int k = 6; int n = sizeof(arr) / sizeof(arr[0]); if (convertGcd(arr, n, k) == true) cout << "Yes"; else cout << "No"; return 0; }

## Output

Yes

- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C++?
- Find the minimum value to be added so that array becomes balanced in C++
- Array with GCD of any of its subset belongs to the given array?
- Add minimum number to an array so that the sum becomes even in C programming
- How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Convert the undirected graph into directed graph such that there is no path of length greater than 1 in C++
- How can I plot a histogram such that the heights of the bars sum to 1 in matplotlib?
- Maximize the sum of array by multiplying prefix of array with -1 in C++
- Return the average of the masked array elements axis 1 in Numpy
- Return an ndarray of indices that sort the masked array along axis 1 in NumPy
- GCD of an array of numbers in java
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Find minimum value to assign all array elements so that array product becomes greater in C++
- Compute the maximum of the masked array elements over axis 1 in Numpy

Advertisements