VOOZH about

URL: https://www.geeksforgeeks.org/dsa/common-divisors-of-n-numbers/

⇱ Common divisors of N numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Common divisors of N numbers

Last Updated : 12 Jul, 2025

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:
Explanation: 
GCD of all the numbers is 1. 
Hence there is only one common divisor of all the numbers i.e., 1.
 


 


Approach: 
 

  1. 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[].
  2. 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: 
 


Output: 
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)
 

Comment
Article Tags: