Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmLargest number dividing maximum number of elements in the array

Largest number dividing maximum number of elements in the array

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:
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:

Approach: A straightforward 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 straightforward 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
// from 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;
}


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the largest number
    // that divides the maximum elements
    // from 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


Python3




# Python3 implementation of the approach
from math import gcd as __gcd
 
# Function to return the largest number
# that divides the maximum elements
# from 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


C#




// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the largest number
    // that divides the maximum elements
    // from 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


Javascript




<script>
// javascript implementation of the approach   
// Function to return the largest number
    // that divides the maximum elements
    // from the given array
    function findLargest(arr , n) {
 
        // Finding gcd of all the numbers
        // in the array
        var gcd = 0;
        for (i = 0; i < n; i++)
            gcd = __gcd(arr[i], gcd);
        return gcd;
    }
 
    function __gcd(a , b) {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Driver code
     
        var arr = [ 3, 6, 9 ];
        var n = arr.length;
 
        document.write(findLargest(arr, n));
 
// This code contributed by umadevi9616
</script>


Output

3

Time Complexity: O(N * log(MAX)), where N is the size of the array and MAX is the maximum element of the array.
Auxiliary Space: O(log(MAX))

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments