Given an array **arr[]** of length **N**, the task is to find the largest number dividing the maximum number of elements from the array.

**Examples:**

Input:arr[] = {2, 12, 6}

Output:2

1 and 2 are the only integers which divide the

maximum number of elements from the array

(i.e. all the elements) and 2 is

the maximum among them.

Input:arr[] = {1, 7, 9}

Output:1

**Approach:** A straight forward approach for solving this problem will be taking the GCD of all the elements. Why this approach works? 1 is the number that divides all the elements of the array. Now, any other number greater than 1 will either divide all the elements of the array (in this case, the number itself is the answer) or it will divide a subset of the array i.e. 1 is the answer here as it divides more elements from the array. So, the most straight forward way for doing this will be to take the GCD of all the elements of the array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the largest number ` `// that divides the maximum elements ` `// fom the given array ` `int` `findLargest(` `int` `* arr, ` `int` `n) ` `{ ` ` ` ` ` `// Finding gcd of all the numbers ` ` ` `// in the array ` ` ` `int` `gcd = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `gcd = __gcd(arr[i], gcd); ` ` ` `return` `gcd; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 6, 9 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << findLargest(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the largest number ` `// that divides the maximum elements ` `// fom the given array ` `static` `int` `findLargest(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// Finding gcd of all the numbers ` ` ` `// in the array ` ` ` `int` `gcd = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `gcd = __gcd(arr[i], gcd); ` ` ` `return` `gcd; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == ` `0` `? a : __gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `3` `, ` `6` `, ` `9` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(findLargest(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd as __gcd ` ` ` `# Function to return the largest number ` `# that divides the maximum elements ` `# fom the given array ` `def` `findLargest(arr, n): ` ` ` ` ` `# Finding gcd of all the numbers ` ` ` `# in the array ` ` ` `gcd ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `gcd ` `=` `__gcd(arr[i], gcd) ` ` ` `return` `gcd ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `3` `, ` `6` `, ` `9` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `print` `(findLargest(arr, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the largest number ` `// that divides the maximum elements ` `// fom the given array ` `static` `int` `findLargest(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` ` ` `// Finding gcd of all the numbers ` ` ` `// in the array ` ` ` `int` `gcd = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `gcd = __gcd(arr[i], gcd); ` ` ` `return` `gcd; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == 0 ? a : __gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 3, 6, 9 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(findLargest(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Smallest number dividing minimum number of elements in the Array
- Smallest number dividing minimum number of elements in the array | Set 2
- Maximum number of contiguous array elements with same number of set bits
- Largest number in given Array formed by repeatedly combining two same elements
- Find the largest number smaller than integer N with maximum number of set bits
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Smallest number to make Array sum at most K by dividing each element
- Find a number that divides maximum array elements
- Count the maximum number of elements that can be selected from the array
- Find integers that divides maximum number of elements of the array
- Maximum number of consecutive 1's in binary representation of all the array elements
- Maximum number of elements greater than X after equally distributing subset of array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Count number of digits after decimal on dividing a number
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Maximum sum of the array after dividing it into three segments
- Maximum number formed from array with K number of adjacent swaps allowed
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Number of ways to calculate a target number using only array elements
- Length of largest subarray whose all elements Powerful 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.