Common divisors of N numbers

26 Jan, 2022
Given an array arr[] of N integers. The task is to find all the common divisors of all N integers.Examples  
Input: arr[] = {6, 90, 12, 18, 30, 18} Output: 1 2 3 6 Explanation: GCD of all the numbers is 6. Now to find all the divisors of 6, we have 6 = 1 * 6 6 = 2 * 3 Hence 1, 2, 3 and 6 the common divisors of {6, 90, 12, 18, 20, 18}. Input: arr[] = {1, 2, 3, 4, 5} Output: 1 Explanation: GCD of all the numbers is 1. Hence there is only one common divisor of all the numbers i.e., 1. 
 
Approach:  
To find the common divisors of all the N integers in the given array arr[] find the greatest common divisors (gcd) of all the integers in arr[].Find all the divisors of greatest common divisors (gcd) obtained in the above step using the approach discussed in this article.
To find the common divisors of all the N integers in the given array arr[] find the greatest common divisors (gcd) of all the integers in arr[].
Find all the divisors of greatest common divisors (gcd) obtained in the above step using the approach discussed in this article.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to find all common// divisors of N numbers#include <bits/stdc++.h>using namespace std; // Function to calculate gcd of// two numbersint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to print all the// common divisorsvoid printAllDivisors(int arr[], int N){    // Variable to find the gcd    // of N numbers    int g = arr[0];     // Set to store all the    // common divisors    set<int> divisors;     // Finding GCD of the given    // N numbers    for (int i = 1; i < N; i++) {        g = gcd(arr[i], g);    }     // Finding divisors of the    // HCF of n numbers    for (int i = 1; i * i <= g; i++) {        if (g % i == 0) {            divisors.insert(i);            if (g / i != i)                divisors.insert(g / i);        }    }     // Print all the divisors    for (auto& it : divisors)        cout << it << " ";} // Driver's Codeint main(){    int arr[] = { 6, 90, 12, 18, 30, 24 };    int n = sizeof(arr) / sizeof(arr[0]);     // Function to print all the    // common divisors    printAllDivisors(arr, n);    return 0;}
// Java program to find all common// divisors of N numbersimport java.util.*; class GFG{ // Function to calculate gcd of// two numbersstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to print all the// common divisorsstatic void printAllDivisors(int arr[], int N){    // Variable to find the gcd    // of N numbers    int g = arr[0];     // Set to store all the    // common divisors    HashSet<Integer> divisors = new HashSet<Integer>();     // Finding GCD of the given    // N numbers    for (int i = 1; i < N; i++)    {        g = gcd(arr[i], g);    }     // Finding divisors of the    // HCF of n numbers    for (int i = 1; i * i <= g; i++)    {        if (g % i == 0)        {            divisors.add(i);            if (g / i != i)                divisors.add(g / i);        }    }     // Print all the divisors    for (int it : divisors)        System.out.print(it+ " ");} // Driver's Codepublic static void main(String[] args){    int arr[] = { 6, 90, 12, 18, 30, 24 };    int n = arr.length;     // Function to print all the    // common divisors    printAllDivisors(arr, n);}} // This code is contributed by 29AjayKumar
# Python3 program to find all common# divisors of N numbers # Function to calculate gcd of# two numbersdef gcd(a, b):    if (a == 0):        return b    return gcd(b % a, a) # Function to print all the# common divisorsdef printAllDivisors(arr, N):         # Variable to find the gcd    # of N numbers    g = arr[0]     # Set to store all the    # common divisors    divisors = dict()     # Finding GCD of the given    # N numbers    for i in range(1, N):        g = gcd(arr[i], g)     # Finding divisors of the    # HCF of n numbers    for i in range(1, g + 1):        if i*i > g:            break        if (g % i == 0):            divisors[i] = 1            if (g // i != i):                divisors[g // i] = 1     # Print all the divisors    for it in sorted(divisors):        print(it, end=" ") # Driver's Codeif __name__ == '__main__':    arr= [6, 90, 12, 18, 30, 24]    n = len(arr)     # Function to print all the    # common divisors    printAllDivisors(arr, n) # This code is contributed by mohit kumar 29
// C# program to find all common// divisors of N numbersusing System;using System.Collections.Generic; class GFG{ // Function to calculate gcd of// two numbersstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to print all the// common divisorsstatic void printAllDivisors(int []arr, int N){    // Variable to find the gcd    // of N numbers    int g = arr[0];     // Set to store all the    // common divisors    HashSet<int> divisors = new HashSet<int>();     // Finding GCD of the given    // N numbers    for (int i = 1; i < N; i++)    {        g = gcd(arr[i], g);    }     // Finding divisors of the    // HCF of n numbers    for (int i = 1; i * i <= g; i++)    {        if (g % i == 0)        {            divisors.Add(i);            if (g / i != i)                divisors.Add(g / i);        }    }     // Print all the divisors    foreach (int it in divisors)        Console.Write(it+ " ");} // Driver's Codepublic static void Main(String[] args){    int []arr = { 6, 90, 12, 18, 30, 24 };    int n = arr.Length;     // Function to print all the    // common divisors    printAllDivisors(arr, n);}} // This code is contributed by PrinciRaj1992
<script>// Javascript program to find all common// divisors of N numbers // Function to calculate gcd of// two numbersfunction gcd(a,b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to print all the// common divisorsfunction printAllDivisors(arr, N){     // Variable to find the gcd    // of N numbers    let g = arr[0];       // Set to store all the    // common divisors    let divisors = new Set();       // Finding GCD of the given    // N numbers    for (let i = 1; i < N; i++)    {        g = gcd(arr[i], g);    }       // Finding divisors of the    // HCF of n numbers    for (let i = 1; i * i <= g; i++)    {        if (g % i == 0)        {            divisors.add(i);            if (Math.floor(g / i) != i)                divisors.add(Math.floor(g / i));        }    }       // Print all the divisors    for (let it of divisors.values())        document.write(it+ " ");} // Driver's Codelet arr=[6, 90, 12, 18, 30, 24];let n = arr.length; // Function to print all the// common divisorsprintAllDivisors(arr, n); // This code is contributed by unknown2108</script>
1 2 3 6
 
Time Complexity: O(N*log(M)) where N is the length of the given array and M is the maximum element in the array.
Auxiliary Space: O((log(max(a, b)))3/2) 
29AjayKumar
princiraj1992
mohit kumar 29
unknown2108
subham348
adnanirshad158
divisors
GCD-LCM
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Next Greater Element
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7