Count of integers that divide all the elements of the given array
Given an array arr[] of N elements. The task is to find the count of positive integers that divide all the array elements.
Examples:
Input: arr[] = {2, 8, 10, 6}
Output: 2
1 and 2 are the only integers that divide
all the elements of the given array.
Input:arr[] = {6, 12, 18, 12, 6}
Output: 4
Approach: We know that the maximum integer that will divide all the array elements will be the gcd of the array and all the other integers that will divide all the elements of the array will have to be the factors of this gcd. Hence, the count of valid integers will be equal to the count of factors of the gcd of all the array elements.
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 count of // the required integers int getCount(int a[], int n) { // To store the gcd of the array elements int gcd = 0; for (int i = 0; i < n; i++) gcd = __gcd(gcd, a[i]); // To store the count of factors // of the found gcd int cnt = 0; for (int i = 1; i * i <= gcd; i++) { if (gcd % i == 0) { // If g is a perfect square if (i * i == gcd) cnt++; // Factors appear in pairs else cnt += 2; } } return cnt; } // Driver code int main() { int a[] = { 4, 16, 1024, 48 }; int n = sizeof(a) / sizeof(a[0]); cout << getCount(a, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Recursive function to return gcd static int calgcd(int a, int b) { if (b == 0) return a; return calgcd(b, a % b); } // Function to return the count of // the required integers static int getCount(int [] a, int n) { // To store the gcd of the array elements int gcd = 0; for (int i = 0; i < n; i++) gcd = calgcd(gcd, a[i]); // To store the count of factors // of the found gcd int cnt = 0; for (int i = 1; i * i <= gcd; i++) { if (gcd % i == 0) { // If g is a perfect square if (i * i == gcd) cnt++; // Factors appear in pairs else cnt += 2; } } return cnt; } // Driver code public static void main (String[] args) { int [] a = { 4, 16, 1024, 48 }; int n = a.length; System.out.println(getCount(a, n)); } } // This code is contributed by ihritik |
Python3
# Python3 implementation of the approach # Function to return the count of # the required integers from math import gcd as __gcd def getCount(a, n): # To store the gcd of the array elements gcd = 0 for i in range(n): gcd = __gcd(gcd, a[i]) # To store the count of factors # of the found gcd cnt = 0 for i in range(1, gcd + 1): if i * i > gcd: break if (gcd % i == 0): # If g is a perfect square if (i * i == gcd): cnt += 1 # Factors appear in pairs else: cnt += 2 return cnt # Driver code a = [4, 16, 1024, 48] n = len(a) print(getCount(a, n)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Recursive function to return gcd static int calgcd(int a, int b) { if (b == 0) return a; return calgcd(b, a % b); } // Function to return the count of // the required integers static int getCount(int [] a, int n) { // To store the gcd of the array elements int gcd = 0; for (int i = 0; i < n; i++) gcd = calgcd(gcd, a[i]); // To store the count of factors // of the found gcd int cnt = 0; for (int i = 1; i * i <= gcd; i++) { if (gcd % i == 0) { // If g is a perfect square if (i * i == gcd) cnt++; // Factors appear in pairs else cnt += 2; } } return cnt; } // Driver code public static void Main () { int [] a = { 4, 16, 1024, 48 }; int n = a.Length; Console.WriteLine(getCount(a, n)); } } // This code is contributed by ihritik |
3
Recommended Posts:
- Divide every element of one array by other array elements
- Count number of ways to divide an array into two halves with same sum
- Find integers that divides maximum number of elements of the array
- Divide two integers without using multiplication, division and mod operator | Set2
- Count of elements that can be deleted without disturbing the mean of the initial array
- Count arrays of length K whose product of elements is same as that of given array
- Count numbers in a range that are divisible by all array elements
- Count digits in given number N which divide N
- Count ways to divide circle using N non-intersecting chord | Set-2
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Count integers in the range [A, B] that are not divisible by C and D
- Count of integers of the form (2^x * 3^y) in the range [L, R]
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count ways to express even number ‘n’ as sum of even integers
- Count of all possible pairs of disjoint subsets of integers from 1 to N
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.

