# Minimum deletions required to make GCD of the array equal to 1

Given an array **arr[]** of **N** integers, the task is to find the minimum deletions required to make the GCD of the resulting array elements equal to **1**. If it is impossible then print **-1**.**Examples:**

Input:arr[] = {2, 4, 6, 3}Output:0

It is clear that GCD(2, 4, 6, 3) = 1

So, we do not need to delete any elements.Input:arr[] = {8, 14, 16, 26}Output:-1

No matter how many elements get deleted, the gcd will never be 1.

**Approach:** If the GCD of the initial array is 1 then we do not need to delete any of the elements and the result will be 0. If GCD is not 1 then the GCD can never be 1 no matter what elements we delete. Let’s say **gcd** be the gcd of the elements of the array and we delete **k** elements. Now, **N – k** elements are left and they still have **gcd** as their factor. So, it is impossible to get GCD of the array elements equal to **1**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to return the minimum` `// deletions required` `int` `MinDeletion(` `int` `a[], ` `int` `n)` `{` ` ` `// To store the GCD of the array` ` ` `int` `gcd = 0;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `gcd = __gcd(gcd, a[i]);` ` ` `// GCD cannot be 1` ` ` `if` `(gcd > 1)` ` ` `return` `-1;` ` ` `// GCD of the elements is already 1` ` ` `else` ` ` `return` `0;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `a[] = { 3, 6, 12, 81, 9 };` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]);` ` ` `cout << MinDeletion(a, n);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the approach` `import` `java.io.*;` `class` `GFG` `{` ` ` `// Recursive function to return gcd of a and b` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b)` ` ` `{` ` ` `// Everything divides 0` ` ` `if` `(a == ` `0` `)` ` ` `return` `b;` ` ` `if` `(b == ` `0` `)` ` ` `return` `a;` ` ` ` ` `// base case` ` ` `if` `(a == b)` ` ` `return` `a;` ` ` ` ` `// a is greater` ` ` `if` `(a > b)` ` ` `return` `__gcd(a-b, b);` ` ` `return` `__gcd(a, b-a);` ` ` `}` ` ` `// Function to return the minimum` ` ` `// deletions required` ` ` `static` `int` `MinDeletion(` `int` `a[], ` `int` `n)` ` ` `{` ` ` ` ` `// To store the GCD of the array` ` ` `int` `gcd = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `gcd = __gcd(gcd, a[i]);` ` ` ` ` `// GCD cannot be 1` ` ` `if` `(gcd > ` `1` `)` ` ` `return` `-` `1` `;` ` ` ` ` `// GCD of the elements is already 1` ` ` `else` ` ` `return` `0` `;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `a[] = { ` `3` `, ` `6` `, ` `12` `, ` `81` `, ` `9` `};` ` ` `int` `n = a.length;` ` ` `System.out.print(MinDeletion(a, n));` ` ` `}` `}` `// This code is contributed by anuj_67..` |

## Python3

`# Python3 implementation of the approach` `from` `math ` `import` `gcd` `# Function to return the minimum` `# deletions required` `def` `MinDeletion(a, n) :` ` ` `# To store the GCD of the array` ` ` `__gcd ` `=` `0` `;` ` ` `for` `i ` `in` `range` `(n) :` ` ` `__gcd ` `=` `gcd(__gcd, a[i]);` ` ` `# GCD cannot be 1` ` ` `if` `(__gcd > ` `1` `) :` ` ` `return` `-` `1` `;` ` ` `# GCD of the elements is already 1` ` ` `else` `:` ` ` `return` `0` `;` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `a ` `=` `[ ` `3` `, ` `6` `, ` `12` `, ` `81` `, ` `9` `];` ` ` `n ` `=` `len` `(a)` ` ` `print` `(MinDeletion(a, n));` `# This code is contributed by AnkitRai01` |

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG` `{` ` ` `// Recursive function to return gcd of a and b` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b)` ` ` `{` ` ` `// Everything divides 0` ` ` `if` `(a == 0)` ` ` `return` `b;` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` ` ` `// base case` ` ` `if` `(a == b)` ` ` `return` `a;` ` ` ` ` `// a is greater` ` ` `if` `(a > b)` ` ` `return` `__gcd(a-b, b);` ` ` `return` `__gcd(a, b-a);` ` ` `}` ` ` `// Function to return the minimum` ` ` `// deletions required` ` ` `static` `int` `MinDeletion(` `int` `[]a, ` `int` `n)` ` ` `{` ` ` ` ` `// To store the GCD of the array` ` ` `int` `gcd = 0;` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `gcd = __gcd(gcd, a[i]);` ` ` ` ` `// GCD cannot be 1` ` ` `if` `(gcd > 1)` ` ` `return` `-1;` ` ` ` ` `// GCD of the elements is already 1` ` ` `else` ` ` `return` `0;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `int` `[]a = { 3, 6, 12, 81, 9 };` ` ` `int` `n = a.Length;` ` ` `Console.WriteLine(MinDeletion(a, n));` ` ` `}` `}` `// This code is contributed by anuj_67..` |

## Javascript

`<script>` `// javascript implementation of the approach` ` ` `// Recursive function to return gcd of a and b` ` ` `function` `__gcd(a , b) {` ` ` `// Everything divides 0` ` ` `if` `(a == 0)` ` ` `return` `b;` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `// base case` ` ` `if` `(a == b)` ` ` `return` `a;` ` ` `// a is greater` ` ` `if` `(a > b)` ` ` `return` `__gcd(a - b, b);` ` ` `return` `__gcd(a, b - a);` ` ` `}` ` ` `// Function to return the minimum` ` ` `// deletions required` ` ` `function` `MinDeletion(a , n) {` ` ` `// To store the GCD of the array` ` ` `var` `gcd = 0;` ` ` `for` `(i = 0; i < n; i++)` ` ` `gcd = __gcd(gcd, a[i]);` ` ` `// GCD cannot be 1` ` ` `if` `(gcd > 1)` ` ` `return` `-1;` ` ` `// GCD of the elements is already 1` ` ` `else` ` ` `return` `0;` ` ` `}` ` ` `// Driver code` ` ` ` ` `var` `a = [ 3, 6, 12, 81, 9 ];` ` ` `var` `n = a.length;` ` ` `document.write(MinDeletion(a, n));` `// This code is contributed by aashish1995` `</script>` |

**Output:**

-1

**Time Complexity:** 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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.