Open In App

GCD of more than two (or array) numbers

Last Updated : 11 Feb, 2025
Suggest changes
Share
Like Article
Like
Report

Given an array arr[] of non-negative numbers, the task is to 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

Input: arr[] = [2, 4, 6, 8]
Output: 2

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.

C++
// C++ program to find GCD of two or // more numbers #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; } 
C
// C program to find GCD of two or // more numbers #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
// Java program to find GCD of two or // more numbers import java.util.*; public class GCDArray {  // 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
# Python program to find GCD of two or # more numbers 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)) 
C#
// C# program to find GCD of two or // more numbers using System; using System.Collections.Generic; class GCDArray {  // 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
// JavaScript program to find GCD of two or // more numbers // 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 version of the above solution

C++
// C++ program to find GCD of two or more numbers #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; } 
C
// C program to find GCD of two or more numbers  #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
// Java program to find GCD of two or more numbers 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
# Function to find GCD of two numbers 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 # Main function if __name__ == '__main__': arr = [2, 4, 6, 8] print(GcdOfArray(arr)) 
C#
// C# program to find GCD of two or more numbers using System; using System.Linq; class GCD {    // 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
// JavaScript program to find GCD of two or more numbers 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++, Python, C#, etc., have built-in methods for calculating the GCD of two numbers.

C++
// C++ program to find GCD of two or more numbers  // Using built-in function #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 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 };  cout << GcdOfArray(arr) << "\n";  return 0; } 
Java
// Java program to find GCD of two or more numbers  // Using built-in function import java.util.*; public class GCDArray {  // Function to return gcd of a and b  public static int GcdOfArray(int[] array) {  int res = array[0];  for (int i = 1; i < array.length; i++) {  // Find gcd(a, b) using inbuilt library function  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;  }  // Helper function for GCD using inbuilt method  public static int gcd(int a, int b) {  return b == 0 ? a : gcd(b, a % b);  }  public static void main(String[] args) {  int[] array = {2, 4, 6, 8};  System.out.println(GcdOfArray(array));  } } 
Python
# Python program to find GCD of two or more numbers  # Using built-in function 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 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] print(GcdOfArray(array)) 
C#
// C# program to find GCD of two or more numbers  // Using built-in function using System; class GCDArray {  // Function to return gcd of a and b  public static int GcdOfArray(int[] array) {  int res = array[0];  for (int i = 1; i < array.Length; i++) {  // Find gcd(a, b) using inbuilt library function  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;  }  // Helper method for GCD  public static int Gcd(int a, int b) {  return b == 0 ? a : Gcd(b, a % b);  }  public static void Main(string[] args) {  int[] array = {2, 4, 6, 8};  Console.WriteLine(GcdOfArray(array));  } } 
JavaScript
// JavaScript program to find GCD of two or more numbers  // Using built-in function // 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 becomes 1 at any iteration then it remains 1  // So no need to check further  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)

 


Next Article

Similar Reads