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.. |
-1
Time Complexity: O(N)
Recommended Posts:
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum operations required to make every element greater than or equal to K
- Minimum number of cuts required to make circle segments equal sized
- Minimum deletions required such that any number X will occur exactly X times
- Minimum number of changes required to make the given array an AP
- Minimum operations required to make all the elements distinct in an array
- Minimum array insertions required to make consecutive difference <= K
- Make array elements equal in Minimum Steps
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum Increment / decrement to make array elements equal
- Minimum number of pairs required to make two strings same
- Minimum number of given moves required to make N divisible by 25
- Minimum swaps required to make a binary string alternating
- Minimum operations of the given type required to make a complete graph
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.



