GCD of more than two (or array) numbers (original) (raw)
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 **i thiteration, 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.
C++ `
#include #include 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 &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 arr = {2, 4, 6, 8, 16};
cout << findGCD(arr) << endl;
return 0;}
C
#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;}
Java
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));
}}
Python
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 resif name == "main": array = [2, 4, 6, 8, 16]
print(findGCD(array))C#
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));
}}
JavaScript
// 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));
`
**Time Complexity: O(n * log(x)), where x is the largest element of the array
**Auxiliary Space: O(1)
Iterative Method
C++ `
#include #include 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& arr) { int gcd = arr[0]; for (int i = 1; i < arr.size(); i++) { gcd = getGCD(gcd, arr[i]); } return gcd; }
int main() { vector arr = { 2, 4, 6, 8 }; cout << GcdOfArray(arr) << "\n"; return 0; }
C
#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; }
Java
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));
}}
Python
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))
C#
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));
}}
JavaScript
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));
`
**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.
C++ `
#include #include #include using namespace std;
// Function to return gcd of a and b int GcdOfArray(vector& 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 arr = { 2, 4, 6, 8 }; cout << GcdOfArray(arr) << "\n"; return 0; }
Java
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));
}}
Python
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 resif name == "main": array = [2, 4, 6, 8] print(GcdOfArray(array))
C#
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));
}}
JavaScript
// 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));
`
**Time Complexity: O(n * log(x)), where x is the largest element of the array
**Auxiliary Space: O(1)