Given an array arr[] of n positive integers, find the GCD of all the array elements.
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
[Naive Approach] Recursive Euclid - O(n log M) Time and O(log M) Space
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 maintaining the GCD of all the elements processed so far. At each iteration, we update this result by recursively calculating the GCD of the current element and the previous result.
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to find GCD of two numbers
int findGCD(int a, int b)
{
if (a == 0)
return b;
return findGCD(b % a, a);
}
// Function to find GCD of the array
int gcd(int n, vector<int> arr)
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
res = findGCD(arr[i], res);
// If GCD becomes 1, it cannot decrease further
if (res == 1)
return 1;
}
return res;
}
int main()
{
vector<int> arr = {2, 4, 6, 8};
cout << gcd(arr.size(), arr) << endl;
return 0;
}
#include <stdio.h>
// Recursive function to find GCD of two numbers
int findGCD(int a, int b)
{
if (a == 0)
return b;
return findGCD(b % a, a);
}
// Function to find GCD of the array
int gcd(int n, int arr[])
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
res = findGCD(arr[i], res);
// If GCD becomes 1, it cannot decrease further
if (res == 1)
return 1;
}
return res;
}
int main()
{
int arr[] = {2, 4, 6, 8};
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", gcd(size, arr));
return 0;
}
public class GFG {
// Recursive function to find GCD of two numbers
public static int findGCD(int a, int b)
{
if (a == 0)
return b;
return findGCD(b % a, a);
}
// Function to find GCD of the array
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
res = findGCD(arr[i], res);
// If GCD becomes 1, it cannot decrease further
if (res == 1)
return 1;
}
return res;
}
public static void main(String[] args)
{
int[] arr = { 2, 4, 6, 8 };
System.out.println(gcd(arr.length, arr));
}
}
# Recursive function to find GCD of two numbers
def findGCD(a, b):
if a == 0:
return b
return findGCD(b % a, a)
# Function to find GCD of the array
def gcd(n, arr):
res = arr[0]
for i in range(1, n):
res = findGCD(arr[i], res)
# If GCD becomes 1, it cannot decrease further
if res == 1:
return 1
return res
if __name__ == '__main__':
arr = [2, 4, 6, 8]
print(gcd(len(arr), arr))
using System;
public class GFG {
// Recursive function to find GCD of two numbers
public static int FindGCD(int a, int b)
{
if (a == 0)
return b;
return FindGCD(b % a, a);
}
// Function to find GCD of the array
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
res = FindGCD(arr[i], res);
// If GCD becomes 1, it cannot decrease further
if (res == 1)
return 1;
}
return res;
}
public static void Main()
{
int[] arr = { 2, 4, 6, 8 };
Console.WriteLine(gcd(arr.Length, arr));
}
}
function findGCD(a, b)
{
if (b === 0)
return a;
return this.findGCD(b, a % b);
}
// Function to find GCD of the array
function gcd(arr, n)
{
let res = 0;
for (let i = 0; i < n; i++) {
res = this.findGCD(arr[i], res);
// If GCD becomes 1, return immediately
if (res === 1)
return 1;
}
return res;
}
// Driver Code
const arr = [ 2, 4, 6, 8 ];
console.log(gcd(arr.length, arr));
Output
2
[Expected Approach] Iterative Euclid - O(n * log(M)) Time and O(1) Space
The idea is to find the GCD of the array elements one by one using the iterative Euclidean algorithm.
We maintain the GCD of the processed elements and update it with each new element. If the GCD becomes 1, we return it immediately.
Working of Approach:
- Initialize res with the first element of the array.
- Traverse the remaining elements and calculate the GCD of res and the current element using the iterative Euclidean algorithm.
- Repeat until all elements are processed or res becomes 1.
- If res becomes 1, return it immediately; otherwise, return the final GCD.
#include <iostream>
#include <vector>
using namespace std;
// Iterative function to find GCD of two numbers
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;
}
// Function to find GCD of the array
int gcd(int n, vector<int> arr)
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
res = getGCD(res, arr[i]);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
int main()
{
vector<int> arr = {2, 4, 6, 8, 16};
cout << gcd(arr.size(), arr) << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Iterative function to find GCD of two numbers
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;
}
// Function to find GCD of the array
int gcd(int n, int arr[])
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
res = getGCD(res, arr[i]);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
int main()
{
int arr[] = {2, 4, 6, 8, 16};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", gcd(n, arr));
return 0;
}
import java.util.*;
public class GFG {
// Iterative function to find GCD of two numbers
public static 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;
}
// Function to find GCD of the array
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
res = getGCD(res, arr[i]);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
public static void main(String[] args)
{
int[] arr = { 2, 4, 6, 8, 16 };
System.out.println(gcd(arr.length, arr));
}
}
def getGCD(a, b):
while a > 0 and b > 0:
if a > b:
a = a % b
else:
b = b % a
if a == 0:
return b
return a
def gcd(n, arr):
res = arr[0]
for i in range(1, n):
res = getGCD(res, arr[i])
if res == 1:
return 1
return res
if __name__ == '__main__':
arr = [2, 4, 6, 8, 16]
print(gcd(len(arr), arr))
using System;
public class GFG {
// Iterative function to find GCD of two numbers
public static 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;
}
// Function to find GCD of the array
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
res = getGCD(res, arr[i]);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
public static void Main()
{
int[] arr = { 2, 4, 6, 8, 16 };
Console.WriteLine(gcd(arr.Length, arr));
}
}
function getGCD(a, b) {
while (a > 0 && b > 0) {
if (a > b)
a = a % b;
else
b = b % a;
}
if (a === 0)
return b;
return a;
}
function gcd(arr, n) {
let res = arr[0];
for (let i = 1; i < n; i++) {
res = getGCD(res, arr[i]);
if (res === 1)
return 1;
}
return res;
}
// Driver Code
const arr = [2, 4, 6, 8, 16];
console.log(gcd(arr, arr.length));
Output
2
[Alternate Approach] Built-in Methods - (O(n log M)) Time and O(1) Space
Built-in GCD functions in different languages:
- C: No standard built-in GCD function; implement the Euclidean algorithm.
- C++: __gcd(a, b) from <algorithm>
- Java: BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))
- Python: math.gcd(a, b) from the math module
- C#: No standard built-in GCD function for integers; implement the Euclidean algorithm.
- JavaScript: No built-in GCD function; implement the Euclidean algorithm.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int gcd(int n, vector<int> arr)
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
// Find GCD using the inbuilt library function
res = __gcd(arr[i], res);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
int main()
{
vector<int> arr = {2, 4, 6, 8};
cout << gcd(arr.size(), arr) << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int gcd_array(int n, int arr[])
{
int res = arr[0];
for (int i = 1; i < n; i++)
{
// Find GCD using the custom function
res = gcd(arr[i], res);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
int main()
{
int arr[] = {2, 4, 6, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", gcd_array(n, arr));
return 0;
}
import java.util.Arrays;
public class GFG {
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
// Find GCD using the custom function
res = gcd(arr[i], res);
// If GCD becomes 1, return immediately
if (res == 1)
return 1;
}
return res;
}
public static void main(String[] args)
{
int[] arr = { 2, 4, 6, 8 };
System.out.println(gcd(arr.length, arr));
}
}
def findGCD(a, b):
if b == 0:
return a
return findGCD(b, a % b)
def gcd(n, arr):
res = arr[0]
for i in range(1, n):
res = findGCD(res, arr[i])
# If GCD becomes 1, return immediately
if res == 1:
return 1
return res
if __name__ == '__main__':
arr = [2, 4, 6, 8]
print(gcd(len(arr), arr))
using System;
class GFG {
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static int gcd(int n, int[] arr)
{
int res = arr[0];
for (int i = 1; i < n; i++) {
res = GCD(res, arr[i]);
if (res == 1)
return 1;
}
return res;
}
public static void Main()
{
int[] arr = { 2, 4, 6, 8 };
Console.WriteLine(gcd(arr.Length, arr));
}
}
function findGCD(a, b) {
if (b === 0)
return a;
return findGCD(b, a % b);
}
function gcd(arr, n) {
let res = arr[0];
for (let i = 1; i < n; i++) {
res = findGCD(arr[i], res);
// If GCD becomes 1, return immediately
if (res === 1)
return 1;
}
return res;
}
// Driver Code
const arr = [2, 4, 6, 8];
console.log(gcd(arr, arr.length));
Output
2
Note: C, C#, and JavaScript do not provide a standard built-in GCD function. Therefore, we can implement the Euclidean algorithm to calculate the GCD of two numbers.