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++
// 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 exists 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 Function int main() { int arr[] = { 2, 2, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << findNumber(arr, n) << endl; return 0; } |
Java
// JAVA program to find such number in // the array that all array elements // are divisible by it import 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
# Python3 program to find such number # in the array that all array # elements are divisible by it # Returns GCD of two numbers def gcd (a, b) : if (a == 0) : return b return gcd (b % a, a) # Function to return the desired # number if exists def 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 Code arr = [2, 2, 4]; n = len(arr) print(findNumber(arr, n)) # This code is contributed by Nikita Tiwari |
C#
// C# program to find such number in // the array that all array elements // are divisible by it using 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 // PHP 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 $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 ?> |
Output :
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Partitions possible such that the minimum element divides all the other elements of the partition
- Find the index of the element in an array which divides most elements before it
- Find an element which divides the array in two subarrays with equal product
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Smallest integer > 1 which divides every element of the given array
- Greatest divisor which divides all natural number in range [L, R]
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Find the largest composite number that divides N but is strictly lesser than N
- Find the last digit when factorial of A divides factorial of B
- Find maximum power of a number that divides a factorial
- Find a distinct pair (x, y) in given range such that x divides y
- Find the Smallest number that divides X^X
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Largest number that divides x and is co-prime with y
- Section formula (Point that divides a line in given ratio)
- Highest power of two that divides a given number
- Check if a M-th fibonacci number divides N-th fibonacci number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

