Find element in array that divides all array elements - GeeksforGeeks

01 Apr, 2021
Given an array of n non-negative integers. Find such element in the array, that all array elements are divisible by it.Examples :  
Input : arr[] = {2, 2, 4}
Output : 2

Input : arr[] = {2, 1, 3, 1, 6}
Output : 1

Input: arr[] = {2, 3, 5}
Output : -1
 
The approach is to calculate GCD of the entire array and then check if there exist an element equal to the GCD of the array. For calculating the gcd of the entire array we will use Euclidean algorithm.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find such number in the array// that all array elements are divisible by it#include <bits/stdc++.h>using namespace std; // Returns gcd of two numbers.int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Function to return the// desired number if existsint findNumber(int arr[], int n){    // Find GCD of array    int ans = arr[0];    for (int i = 0; i < n; i++)        ans = gcd(ans, arr[i]);     // Check if GCD is present in array    for (int i = 0; i < n; i++)        if (arr[i] == ans)            return ans;     return -1;} // Driver Functionint main(){    int arr[] = { 2, 2, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << findNumber(arr, n) << endl;    return 0;}
// JAVA program to find such number in// the array that all array elements// are divisible by itimport java.io.*; class GFG {     // Returns GCD of two numbers    static int gcd(int a, int b)    {        if (a == 0)            return b;        return gcd(b % a, a);    }     // Function to return the desired    // number if exists    static int findNumber(int arr[], int n)    {        // Find GCD of array        int ans = arr[0];        for (int i = 0; i < n; i++)            ans = gcd(ans, arr[i]);         // Check if GCD is present in array        for (int i = 0; i < n; i++)            if (arr[i] == ans)                return ans;         return -1;    }     // Driver Code    public static void main(String args[])    {        int arr[] = { 2, 2, 4 };        int n = arr.length;        System.out.println(findNumber(arr, n));    }} // This code is contributed by Nikita Tiwari
# Python3 program to find such number# in the array that all array# elements are divisible by it # Returns GCD of two numbersdef gcd (a, b) :    if (a == 0) :        return b         return gcd (b % a, a)     # Function to return the desired# number if existsdef findNumber (arr, n) :     # Find GCD of array    ans = arr[0]    for i in range(0, n) :        ans = gcd (ans, arr[i])             # Check if GCD is present in array    for i in range(0, n) :        if (arr[i] == ans) :            return ans         return -1     # Driver Codearr = [2, 2, 4];n = len(arr)print(findNumber(arr, n)) # This code is contributed by Nikita Tiwari
// C# program to find such number in// the array that all array elements// are divisible by itusing System; class GFG {     // Returns GCD of two numbers    static int gcd(int a, int b)    {        if (a == 0)            return b;        return gcd(b % a, a);    }     // Function to return the desired    // number if exists    static int findNumber(int[] arr, int n)    {        // Find GCD of array        int ans = arr[0];        for (int i = 0; i < n; i++)            ans = gcd(ans, arr[i]);         // Check if GCD is present in array        for (int i = 0; i < n; i++)            if (arr[i] == ans)                return ans;         return -1;    }     // Driver Code    public static void Main()    {        int[] arr = { 2, 2, 4 };        int n = arr.Length;        Console.WriteLine(findNumber(arr, n));    }} // This code is contributed by vt_m
<?php// PHP program to find such// number in the array that// all array elements are// divisible by it // Returns gcd of two numbersfunction gcd ($a, $b){    if ($a == 0)        return $b;    return gcd ($b % $a, $a);} // Function to return the// desired number if existsfunction findNumber ($arr, $n){    // Find GCD of array    $ans = $arr[0];    for ($i = 0; $i < $n; $i++)        $ans = gcd ($ans, $arr[$i]);         // Check if GCD is    // present in array    for ($i = 0; $i < $n; $i++)        if ($arr[$i] == $ans)                    return $ans;         return -1;} // Driver Code$arr =array (2, 2, 4);$n = sizeof($arr);echo findNumber($arr, $n), "\n"; // This code is contributed by ajit?>
<script>     // Javascript program to find such number in the array    // that all array elements are divisible by it         // Returns gcd of two numbers.    function gcd(a, b)    {        if (a == 0)            return b;        return gcd(b % a, a);    }     // Function to return the    // desired number if exists    function findNumber(arr, n)    {        // Find GCD of array        let ans = arr[0];        for (let i = 0; i < n; i++)            ans = gcd(ans, arr[i]);         // Check if GCD is present in array        for (let i = 0; i < n; i++)            if (arr[i] == ans)                return ans;         return -1;    }         let arr = [ 2, 2, 4 ];    let n = arr.length;    document.write(findNumber(arr, n)); </script>
Output : 
2
 
vt_m
jit_t
divyesh072019
divisibility
GCD-LCM
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
C++ Data Types
Coin Change | DP-7