Given an array of numbers, find GCD of 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
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)
For an array of elements, we do the following. We will also check for the result if the result at any step becomes 1 we will just return the 1 as gcd(1,x)=1.
result = arr[0]
For i = 1 to n-1
result = GCD(result, arr[i])
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findGCD( int arr[], int n)
{
int result = arr[0];
for ( int i = 1; i < n; i++)
{
result = gcd(arr[i], result);
if (result == 1)
{
return 1;
}
}
return result;
}
int main()
{
int arr[] = { 2, 4, 6, 8, 16 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findGCD(arr, n) << endl;
return 0;
}
|
Java
public class GCD {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findGCD( int arr[], int n)
{
int result = arr[ 0 ];
for ( int element: arr){
result = gcd(result, element);
if (result == 1 )
{
return 1 ;
}
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 6 , 8 , 16 };
int n = arr.length;
System.out.println(findGCD(arr, n));
}
}
|
Python
def find_gcd(x, y):
while (y):
x, y = y, x % y
return x
l = [ 2 , 4 , 6 , 8 , 16 ]
num1 = l[ 0 ]
num2 = l[ 1 ]
gcd = find_gcd(num1, num2)
for i in range ( 2 , len (l)):
gcd = find_gcd(gcd, l[i])
print (gcd)
|
C#
using System;
public class GCD {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findGCD( int [] arr, int n)
{
int result = arr[0];
for ( int i = 1; i < n; i++){
result = gcd(arr[i], result);
if (result == 1)
{
return 1;
}
}
return result;
}
public static void Main()
{
int [] arr = { 2, 4, 6, 8, 16 };
int n = arr.Length;
Console.Write(findGCD(arr, n));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function findGCD( $arr , $n )
{
$result = $arr [0];
for ( $i = 1; $i < $n ; $i ++){
$result = gcd( $arr [ $i ], $result );
if ( $result == 1)
{
return 1;
}
}
return $result ;
}
$arr = array ( 2, 4, 6, 8, 16 );
$n = sizeof( $arr );
echo (findGCD( $arr , $n ));
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function findGCD(arr, n)
{
let result = arr[0];
for (let i = 1; i < n; i++)
{
result = gcd(arr[i], result);
if (result == 1)
{
return 1;
}
}
return result;
}
let arr = [ 2, 4, 6, 8, 16 ];
let n = arr.length;
document.write(findGCD(arr, n) + "<br>" );
</script>
|
Time Complexity: O(N * log(N)), where N is the largest element of the array
Auxiliary Space: O(1)
Recursive Method: Implementation of Algorithm recursively :
C++
#include <bits/stdc++.h>
using namespace std;
int GcdOfArray(vector< int >& arr, int idx)
{
if (idx == arr.size() - 1) {
return arr[idx];
}
int a = arr[idx];
int b = GcdOfArray(arr, idx + 1);
return __gcd(
a, b);
}
int main()
{
vector< int > arr = { 1, 2, 3 };
cout << GcdOfArray(arr, 0) << "\n" ;
arr = { 2, 4, 6, 8 };
cout << GcdOfArray(arr, 0) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
static int GcdOfArray( int [] arr, int idx)
{
if (idx == arr.length - 1 ) {
return arr[idx];
}
int a = arr[idx];
int b = GcdOfArray(arr, idx + 1 );
return __gcd(
a, b);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
System.out.print(GcdOfArray(arr, 0 )+ "\n" );
int [] arr1 = { 2 , 4 , 6 , 8 };
System.out.print(GcdOfArray(arr1, 0 )+ "\n" );
}
}
|
Python3
import math
def GcdOfArray(arr, idx):
if idx = = len (arr) - 1 :
return arr[idx]
a = arr[idx]
b = GcdOfArray(arr,idx + 1 )
return math.gcd(a, b)
arr = [ 1 , 2 , 3 ]
print (GcdOfArray(arr, 0 ))
arr = [ 2 , 4 , 6 , 8 ]
print (GcdOfArray(arr, 0 ))
|
C#
using System;
public class GCD {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int GcdOfArray( int [] arr, int idx)
{
if (idx==arr.Length-1)
{
return arr[idx];
}
int a = arr[idx];
int b = GcdOfArray(arr, idx + 1);
return gcd(a,b);
}
public static void Main()
{
int [] arr = { 1,2,3 };
Console.Write(GcdOfArray(arr, 0)+ "\n" );
int [] arr1 = { 2,4,6,8 };
Console.Write(GcdOfArray(arr1, 0));
}
}
|
Javascript
function gcd(a, b)
{
if (!b)
return a;
return gcd(b, a % b);
}
function GcdOfArray(arr, idx)
{
if (idx == arr.length - 1) {
return arr[idx];
}
var a = arr[idx];
var b = GcdOfArray(arr, idx + 1);
return gcd(a, b);
}
var arr = [ 1, 2, 3 ];
console.log(GcdOfArray(arr, 0));
arr = [ 2, 4, 6, 8 ];
console.log(GcdOfArray(arr, 0));
|
Time Complexity: O(N * log(N)), where N is the largest element of the array
Auxiliary Space: O(N)
Iterative version of the above solution
C++
#include <bits/stdc++.h>
using namespace std;
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 = 0;
for ( int i = 0; i < arr.size(); i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
int main()
{
vector< int > arr = { 1, 2, 3 };
cout << GcdOfArray(arr) << "\n" ;
arr = { 2, 4, 6, 8 };
cout << GcdOfArray(arr) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
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;
}
static int GcdOfArray( int [] arr)
{
int gcd = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 };
System.out.print(GcdOfArray(arr)+ "\n" );
int [] arr1 = { 2 , 4 , 6 , 8 };
System.out.print(GcdOfArray(arr1)+ "\n" );
}
}
|
Python
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 GcdOfArray(arr):
gcd = 0
for i in range ( len (arr)):
gcd = getGCD(gcd, arr[i])
return gcd
arr = [ 1 , 2 , 3 ]
print (GcdOfArray(arr))
arr = [ 2 , 4 , 6 , 8 ]
print (GcdOfArray(arr))
|
C#
using System;
public class GCD {
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;
}
static int GcdOfArray( int [] arr)
{
int gcd = 0;
for ( int i = 0; i < arr.Length; i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
public static void Main()
{
int [] arr = { 1, 2, 3 };
Console.Write(GcdOfArray(arr) + "\n" );
int [] arr1 = { 2, 4, 6, 8 };
Console.Write(GcdOfArray(arr1));
}
}
|
Javascript
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 GcdOfArray(arr)
{
let gcd = 0;
for (int i = 0; i < arr.size(); i++) {
gcd = getGCD(gcd, arr[i]);
}
return gcd;
}
var arr = [ 1, 2, 3 ];
console.log(GcdOfArray(arr));
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)
This article is contributed by DANISH_RAZA . If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!