Given N numbers, the task is to find the minimum removal of numbers such that GCD of the remaining numbers is greater than initial GCD of N numbers. If it is not possible to increase the GCD, print “NO”.

**Examples:**

Input: a[] = {1, 2, 4}

Output: 1

Remove the first element, then the new GCD is 2, which is greater than the initial GCD i.e., 1.Input: a[] = {6, 9, 15, 30}

Output: 2

The initial gcd is 3, remove 6 and 9 to obtain a gcd of 15 which is greater than 3. You can also remove 9 and 15 to get a gcd of 6.

The following steps are followed to solve the above problem:

- Initially find the gcd of N numbers using Euclidean algorithms.
- Divide all numbers by the obtained gcd.
- Using prime factorization for multiple queries method, find the prime factorization of every number in O(log N). The method can be read here.
- Insert all the prime factors in the set to remove duplicates which are obtained using this method.
- Using a hash-map, count the frequencies of the prime factors in every i-th element.
- Once the factorization of numbers have been done, and the count has been stored in the frequency table, iterate in the hash-map and find out the prime factor which occurs the maximum number of times. It cannot be N, as we have already divided the array elements initially by the initial gcd of N numbers.
- The number of removals will always be n-(hash[prime_factor]) if there are any such factors after division of initial gcd.

Below is the implementation of the above approach.

## CPP

`// C++ program to find the minimum removals ` `// such that gcd of remaining numbers is more ` `// than the initial gcd of N numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAXN 100001 ` ` ` `// stores smallest prime factor for every number ` `int` `spf[MAXN]; ` ` ` `// Calculating SPF (Smallest Prime Factor) for every ` `// number till MAXN. ` `// Time Complexity : O(nloglogn) ` `void` `sieve() ` `{ ` ` ` `spf[1] = 1; ` ` ` `for` `(` `int` `i = 2; i < MAXN; i++) ` ` ` ` ` `// marking smallest prime factor for every ` ` ` `// number to be itself. ` ` ` `spf[i] = i; ` ` ` ` ` `// separately marking spf for every even ` ` ` `// number as 2 ` ` ` `for` `(` `int` `i = 4; i < MAXN; i += 2) ` ` ` `spf[i] = 2; ` ` ` ` ` `for` `(` `int` `i = 3; i * i < MAXN; i++) { ` ` ` ` ` `// checking if i is prime ` ` ` `if` `(spf[i] == i) { ` ` ` ` ` `// marking SPF for all numbers divisible by i ` ` ` `for` `(` `int` `j = i * i; j < MAXN; j += i) ` ` ` ` ` `// marking spf[j] if it is not ` ` ` `// previously marked ` ` ` `if` `(spf[j] == j) ` ` ` `spf[j] = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// A O(log n) function returning primefactorization ` `// by dividing by smallest prime factor at every step ` `vector<` `int` `> getFactorization(` `int` `x) ` `{ ` ` ` `vector<` `int` `> ret; ` ` ` `while` `(x != 1) { ` ` ` `ret.push_back(spf[x]); ` ` ` `x = x / spf[x]; ` ` ` `} ` ` ` `return` `ret; ` `} ` ` ` `// Function which returns the minimal ` `// removals required to make gcd ` `// greater than previous ` `int` `minimumRemovals(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `g = 0; ` ` ` ` ` `// finding initial gcd ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `g = __gcd(a[i], g); ` ` ` ` ` `unordered_map<` `int` `, ` `int` `> mpp; ` ` ` ` ` `// divides all number by initial gcd ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `a[i] = a[i] / g; ` ` ` ` ` `// iterating for all numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// primt factorisation to get the prime ` ` ` `// factors of i-th element in the array ` ` ` `vector<` `int` `> p = getFactorization(a[i]); ` ` ` `set<` `int` `> s; ` ` ` ` ` `// insert all the prime factors in ` ` ` `// set to remove duplicates ` ` ` `for` `(` `int` `j = 0; j < p.size(); j++) { ` ` ` `s.insert(p[j]); ` ` ` `} ` ` ` ` ` `/// increase the count of prime ` ` ` `// factor in map for every element ` ` ` `for` `(` `auto` `it = s.begin(); it != s.end(); it++) { ` ` ` `int` `el = *it; ` ` ` `mpp[el] += 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `int` `mini = INT_MAX; ` ` ` `int` `mini1 = INT_MAX; ` ` ` ` ` `// iterate in map and check for every factor ` ` ` `// and its count ` ` ` `for` `(` `auto` `it = mpp.begin(); it != mpp.end(); it++) { ` ` ` `int` `fir = it->first; ` ` ` `int` `sec = it->second; ` ` ` ` ` `// check for the largest appearing factor ` ` ` `// which does not appears in any one or more ` ` ` `if` `((n - sec) <= mini) { ` ` ` `mini = n - sec; ` ` ` `} ` ` ` `} ` ` ` `if` `(mini != INT_MAX) ` ` ` `return` `mini; ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 6, 9, 15, 30 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `sieve(); ` ` ` `cout << minimumRemovals(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the minimum removals ` `# such that gcd of remaining numbers is more ` `# than the initial gcd of N numbers ` `from` `math ` `import` `gcd as __gcd ` ` ` `MAXN ` `=` `100001` ` ` `# stores smallest prime factor for every number ` `spf ` `=` `[i ` `for` `i ` `in` `range` `(MAXN)] ` ` ` `# Calculating SPF (Smallest Prime Factor) for every ` `# number till MAXN. ` `# Time Complexity : O(nloglogn) ` `def` `sieve(): ` ` ` ` ` `# separately marking spf for every even ` ` ` `# number as 2 ` ` ` `for` `i ` `in` `range` `(` `4` `, MAXN, ` `2` `): ` ` ` `spf[i] ` `=` `2` ` ` ` ` `for` `i ` `in` `range` `(` `3` `, MAXN): ` ` ` ` ` `if` `i ` `*` `i > MAXN: ` ` ` `break` ` ` ` ` `# checking if i is prime ` ` ` `if` `(spf[i] ` `=` `=` `i): ` ` ` ` ` `# marking SPF for all numbers divisible by i ` ` ` `for` `j ` `in` `range` `(` `2` `*` `i, MAXN, i): ` ` ` ` ` `# marking spf[j] if it is not ` ` ` `# previously marked ` ` ` `if` `(spf[j] ` `=` `=` `j): ` ` ` `spf[j] ` `=` `i ` ` ` `# A O(log n) function returning primefactorization ` `# by dividing by smallest prime factor at every step ` `def` `getFactorization(x): ` ` ` `ret ` `=` `[] ` ` ` `while` `(x !` `=` `1` `): ` ` ` `ret.append(spf[x]) ` ` ` `x ` `=` `x ` `/` `/` `spf[x] ` ` ` `return` `ret ` ` ` `# Function which returns the minimal ` `# removals required to make gcd ` `# greater than previous ` `def` `minimumRemovals(a, n): ` ` ` `g ` `=` `0` ` ` ` ` `# finding initial gcd ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `g ` `=` `__gcd(a[i], g) ` ` ` ` ` `mpp ` `=` `dict` `() ` ` ` ` ` `# divides all number by initial gcd ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `a[i] ` `=` `a[i] ` `/` `/` `g ` ` ` ` ` `# iterating for all numbers ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# primt factorisation to get the prime ` ` ` `# factors of i-th element in the array ` ` ` `p ` `=` `getFactorization(a[i]) ` ` ` `s ` `=` `dict` `() ` ` ` ` ` `# insert all the prime factors in ` ` ` `# set to remove duplicates ` ` ` `for` `j ` `in` `range` `(` `len` `(p)): ` ` ` `s[p[j]] ` `=` `1` ` ` ` ` `# increase the count of prime ` ` ` `# factor in map for every element ` ` ` `for` `i ` `in` `s: ` ` ` `mpp[i] ` `=` `mpp.get(i, ` `0` `) ` `+` `1` ` ` ` ` `mini ` `=` `10` `*` `*` `9` ` ` `mini1 ` `=` `10` `*` `*` `9` ` ` ` ` `# iterate in map and check for every factor ` ` ` `# and its count ` ` ` `for` `i ` `in` `mpp: ` ` ` `fir ` `=` `i ` ` ` `sec ` `=` `mpp[i] ` ` ` ` ` `# check for the largest appearing factor ` ` ` `# which does not appears in any one or more ` ` ` `if` `((n ` `-` `sec) <` `=` `mini): ` ` ` `mini ` `=` `n ` `-` `sec ` ` ` ` ` `if` `(mini !` `=` `10` `*` `*` `9` `): ` ` ` `return` `mini ` ` ` `else` `: ` ` ` `return` `-` `1` ` ` `# Driver code ` `a ` `=` `[` `6` `, ` `9` `, ` `15` `, ` `30` `] ` `n ` `=` `len` `(a) ` `sieve() ` `print` `(minimumRemovals(a, n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

2

**
Time Complexity:** O(log log N) for precalculation of Sieve, and O(N * log N) for calculation.

**Auxiliary Space:**O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum removals from array to make max - min <= K
- Minimum removals to make array sum even
- Minimum removals to make array sum odd
- Minimum palindromic subarray removals to make array Empty
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum deletions required to make GCD of the array equal to 1
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- GCD of array is greater than one
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Largest subsequence having GCD greater than 1
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Find minimum GCD of all pairs in an array
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.