Given an array arr[] of non-negative numbers, find GCD of all the array elements. In a previous post we find GCD of two number.
Examples:
Input: arr[] = [1, 2, 3]
Output: 1
Explanation: The GCD of 1, 2, and 3 is 1 because they have no common divisor greater than 1.
Input: arr[] = [2, 4, 6, 8]
Output: 2
Explanation: The GCD of 2, 4, 6, and 8 is 2 because 2 is the greatest number that divides all elements exactly.
Table of Content
Using Recursive GCD
The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.
gcd(a, b, c) = gcd(a, gcd(b, c))
= gcd(gcd(a, b), c)
= gcd(gcd(a, c), b)
We traverse the array while keeping track of a variable that stores the GCD of all the elements processed up to that point. During the ith iteration, we update this GCD by calculating the GCD of the current element and the GCD obtained so far.
We will also check for the result if the result at any step becomes 1 then return 1 as gcd(1, any number) = 1.
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to return gcd of a and b
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of numbers
int findGCD(vector<int> &arr) {
int res = arr[0];
for (int i = 1; i < arr.size(); i++) {
res = gcd(arr[i], res);
// If res becomes 1 at any iteration then it remains 1
// So no need to check further
if (res == 1)
return 1;
}
return res;
}
int main() {
vector<int> arr = {2, 4, 6, 8, 16};
cout << findGCD(arr) << endl;
return 0;
}
#include <stdio.h>
// Recursive function to return gcd of a and b
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of numbers
int findGCD(int array[], int n) {
int res = array[0];
for (int i = 1; i < n; i++) {
res = gcd(array[i], res);
// If res becomes 1 at any iteration then it remains 1
// So no need to check further
if (res == 1)
return 1;
}
return res;
}
int main() {
int array[] = {2, 4, 6, 8, 16};
int n = sizeof(array) / sizeof(array[0]);
printf("%d\n", findGCD(array, n));
return 0;
}
import java.util.*;
public class GFG {
// Recursive function to return gcd of a and b
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of numbers
public static int findGCD(int[] array) {
int res = array[0];
for (int i = 1; i < array.length; i++) {
res = gcd(array[i], res);
// If res becomes 1 at any iteration then it remains 1
// So no need to check further
if (res == 1)
return 1;
}
return res;
}
public static void main(String[] args) {
int[] array = {2, 4, 6, 8, 16};
System.out.println(findGCD(array));
}
}
from functools import reduce
# Recursive function to return gcd of a and b
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
# Function to find gcd of array of numbers
def findGCD(array):
res = array[0]
for num in array[1:]:
res = gcd(num, res)
# If res becomes 1 at any iteration then it remains 1
# So no need to check further
if res == 1:
return 1
return res
if __name__ == "__main__":
array = [2, 4, 6, 8, 16]
print(findGCD(array))
using System;
using System.Collections.Generic;
class GFG {
// Recursive function to return gcd of a and b
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of numbers
public static int findGCD(int[] array) {
int res = array[0];
for (int i = 1; i < array.Length; i++) {
res = gcd(array[i], res);
// If res becomes 1 at any iteration then it remains 1
// So no need to check further
if (res == 1)
return 1;
}
return res;
}
static void Main(string[] args) {
int[] array = {2, 4, 6, 8, 16};
Console.WriteLine(findGCD(array));
}
}
// Recursive function to return gcd of a and b
function gcd(a, b) {
if (a === 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of numbers
function findGCD(array) {
let res = array[0];
for (let i = 1; i < array.length; i++) {
res = gcd(array[i], res);
// If res becomes 1 at any iteration then it remains 1
// So no need to check further
if (res === 1)
return 1;
}
return res;
}
// Driver Code
const array = [2, 4, 6, 8, 16];
console.log(findGCD(array));
Output
2
Time Complexity: O(n * log(x)), where x is the largest element of the array
Auxiliary Space: O(1)
Iterative Method
#include <iostream>
#include <vector>
using namespace std;
// Iterative implementation
int getGCD(int a, int b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
}
else {
b = b % a;
}
}
if (a == 0) {
return b;
}
return a;
}
int GcdOfArray(vector<int>& arr) {
int gcd = arr[0];
for (int i = 1; i < arr.size(); i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
int main() {
vector<int> arr = { 2, 4, 6, 8 };
cout << GcdOfArray(arr) << "\n";
return 0;
}
#include <stdio.h>
// Iterative implementation
int getGCD(int a, int b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
return (a == 0) ? b : a;
}
int GcdOfArray(int arr[], int n) {
int gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
int main() {
int arr[] = {2, 4, 6, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", GcdOfArray(arr, n));
return 0;
}
import java.util.Arrays;
import java.util.List;
class GFG {
// Iterative implementation
static int getGCD(int a, int b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
return (a == 0) ? b : a;
}
static int GcdOfArray(int[] arr) {
int gcd = arr[0];
for (int num : arr) {
gcd = getGCD(gcd, num);
}
return gcd;
}
public static void main(String[] args) {
int[] arr = { 2, 4, 6, 8 };
System.out.println(GcdOfArray(arr));
}
}
def getGcd(a, b):
while a > 0 and b > 0:
if a > b:
a = a % b
else:
b = b % a
return b if a == 0 else a
# Function to find GCD of an array
def GcdOfArray(arr):
gcd = arr[0]
for num in arr:
gcd = getGcd(gcd, num)
return gcd
if __name__ == '__main__':
arr = [2, 4, 6, 8]
print(GcdOfArray(arr))
using System;
using System.Linq;
class GFG {
// Iterative implementation
static int getGCD(int a, int b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
return (a == 0) ? b : a;
}
static int GcdOfArray(int[] arr) {
int gcd = arr[0];
foreach (int num in arr) {
gcd = getGCD(gcd, num);
}
return gcd;
}
public static void Main() {
int[] arr = {2, 4, 6, 8};
Console.WriteLine(GcdOfArray(arr));
}
}
function getGCD(a, b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
return (a === 0) ? b : a;
}
function GcdOfArray(arr) {
let gcd = 0;
for (let num of arr) {
gcd = getGCD(gcd, num);
}
return gcd;
}
// Driver Code
const arr = [2, 4, 6, 8];
console.log(GcdOfArray(arr));
Output
2
Time Complexity: O(n * log(x)), where x is the largest element of the array
Auxiliary Space: O(1)
Using Built-in Methods
Languages like C++, Java, Python, C# etc., have built-in methods for calculating the GCD of two numbers.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to return gcd of a and b
int GcdOfArray(vector<int>& arr) {
int res = arr[0];
for (int i = 1; i < arr.size(); i++) {
// Find gcd(a, b) using inbuilt library function
res = __gcd(arr[i], res);
if (res == 1)
return 1;
}
return res;
}
int main() {
vector<int> arr = { 2, 4, 6, 8 };
cout << GcdOfArray(arr) << "\n";
return 0;
}
import java.util.*;
import java.math.BigInteger;
public class GFG {
// Function to return gcd of array
public static int GcdOfArray(int[] array) {
BigInteger res = BigInteger.valueOf(array[0]);
// Find gcd(a, b) using inbuilt library function
for (int i = 1; i < array.length; i++) {
res = res.gcd(BigInteger.valueOf(array[i]));
if (res.intValue() == 1)
return 1;
}
return res.intValue();
}
public static void main(String[] args) {
int[] array = {2, 4, 6, 8};
System.out.println(GcdOfArray(array));
}
}
from math import gcd
# Function to return gcd of a and b
def GcdOfArray(array):
res = array[0]
for num in array[1:]:
# Find gcd(a, b) using inbuilt library function
res = gcd(num, res)
if res == 1:
return 1
return res
if __name__ == "__main__":
array = [2, 4, 6, 8]
print(GcdOfArray(array))
using System;
using System.Numerics;
class GFG {
// Function to return gcd of a and b
public static int GcdOfArray(int[] array) {
BigInteger res = array[0];
// Find gcd(a, b) using inbuilt library function
for (int i = 1; i < array.Length; i++) {
res = BigInteger.GreatestCommonDivisor(res, array[i]);
if (res == 1)
return 1;
}
return (int)res;
}
public static void Main(string[] args) {
int[] array = {2, 4, 6, 8};
Console.WriteLine(GcdOfArray(array));
}
}
// Function to return gcd of a and b
function GcdOfArray(array) {
let res = array[0];
for (let i = 1; i < array.length; i++) {
// Find gcd(a, b) using inbuilt library function
res = gcd(array[i], res);
if (res === 1)
return 1;
}
return res;
}
// Helper function for GCD
function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
const array = [2, 4, 6, 8];
console.log(GcdOfArray(array));
Output
2
Time Complexity: O(n * log(x)), where x is the largest element of the array
Auxiliary Space: O(1)