Common divisors of N numbers
Last Updated :
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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
void printAllDivisors( int arr[], int N)
{
int g = arr[0];
set< int > divisors;
for ( int i = 1; i < N; i++) {
g = gcd(arr[i], g);
}
for ( int i = 1; i * i <= g; i++) {
if (g % i == 0) {
divisors.insert(i);
if (g / i != i)
divisors.insert(g / i);
}
}
for ( auto & it : divisors)
cout << it << " " ;
}
int main()
{
int arr[] = { 6, 90, 12, 18, 30, 24 };
int n = sizeof (arr) / sizeof (arr[0]);
printAllDivisors(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static void printAllDivisors( int arr[], int N)
{
int g = arr[ 0 ];
HashSet<Integer> divisors = new HashSet<Integer>();
for ( int i = 1 ; i < N; i++)
{
g = gcd(arr[i], g);
}
for ( int i = 1 ; i * i <= g; i++)
{
if (g % i == 0 )
{
divisors.add(i);
if (g / i != i)
divisors.add(g / i);
}
}
for ( int it : divisors)
System.out.print(it+ " " );
}
public static void main(String[] args)
{
int arr[] = { 6 , 90 , 12 , 18 , 30 , 24 };
int n = arr.length;
printAllDivisors(arr, n);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def printAllDivisors(arr, N):
g = arr[ 0 ]
divisors = dict ()
for i in range ( 1 , N):
g = gcd(arr[i], g)
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
for it in sorted (divisors):
print (it, end = " " )
if __name__ = = '__main__' :
arr = [ 6 , 90 , 12 , 18 , 30 , 24 ]
n = len (arr)
printAllDivisors(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void printAllDivisors( int []arr, int N)
{
int g = arr[0];
HashSet< int > divisors = new HashSet< int >();
for ( int i = 1; i < N; i++)
{
g = gcd(arr[i], g);
}
for ( int i = 1; i * i <= g; i++)
{
if (g % i == 0)
{
divisors.Add(i);
if (g / i != i)
divisors.Add(g / i);
}
}
foreach ( int it in divisors)
Console.Write(it+ " " );
}
public static void Main(String[] args)
{
int []arr = { 6, 90, 12, 18, 30, 24 };
int n = arr.Length;
printAllDivisors(arr, n);
}
}
|
Javascript
<script>
function gcd(a,b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function printAllDivisors(arr, N)
{
let g = arr[0];
let divisors = new Set();
for (let i = 1; i < N; i++)
{
g = gcd(arr[i], g);
}
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));
}
}
for (let it of divisors.values())
document.write(it+ " " );
}
let arr=[6, 90, 12, 18, 30, 24];
let n = arr.length;
printAllDivisors(arr, n);
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...