![]() |
VOOZH | about |
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:
Below is the implementation of the above approach:
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)