Find all numbers that divide maximum array elements

Given an array of N numbers, the task is to print all the numbers greater than 1 which divide the maximum of array elements.

Examples:

Input: a[] = {6, 6, 12, 18, 13}
Output: 2 3 6
All the numbers divide the maximum of array elements i.e., 4

Input: a[] = {12, 15, 27, 20, 40}
Output: 2 3 4 5

Approach:



  • Use hashing to store the count of all the factors of every array element. We can find all the factors of number in O(sqrt N).
  • Traverse for all factors, and find the count of maximum array elements which are divided by numbers.
  • Again re-traverse for all factors and print the factors that occur the maximum number of times.

Below is the implementation of the above approach.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all the numbers
// that divides maximum of array elements
#include <bits/stdc++.h>
using namespace std;
  
// Function that prints all the numbers
// which divides maximum of array elements
void printNumbers(int a[], int n)
{
  
    // hash to store the number of times
    // a factor is there
    unordered_map<int, int> mpp;
  
    for (int i = 0; i < n; i++) {
        int num = a[i];
  
        // find all the factors
        for (int j = 1; j * j <= num; j++) {
  
            // if j is factor of num
            if (num % j == 0) {
                if (j != 1)
                    mpp[j]++;
  
                if ((num / j) != j)
                    mpp[num / j]++;
            }
        }
    }
  
    // find the maximum times
    // it can divide
    int maxi = 0;
    for (auto it : mpp) {
        maxi = max(it.second, maxi);
    }
  
    // print all the factors of
    // numbers which divides the
    // maximum array elements
    for (auto it : mpp) {
        if (it.second == maxi)
            cout << it.first << " ";
    }
}
  
// Driver Code
int main()
{
  
    int a[] = { 12, 15, 27, 20, 40 };
    int n = sizeof(a) / sizeof(a[0]);
    printNumbers(a, n);
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to prall the numbers
# that divides maximum of array elements
  
# Function that prints all the numbers
# which divides maximum of array elements
def printNumbers(a, n):
  
    # hash to store the number of times
    # a factor is there
    mpp = dict()
  
    for i in range(n):
        num = a[i]
  
        # find all the factors
        for j in range(1, num + 1):
  
            if j * j > num:
                break
  
            # if j is factor of num
            if (num % j == 0):
                if (j != 1):
                    mpp[j]=mpp.get(j, 0) + 1
  
                if ((num // j) != j):
                    mpp[num // j]=mpp.get(num//j, 0) + 1
              
    # find the maximum times
    # it can divide
    maxi = 0
    for it in mpp:
        maxi = max(mpp[it], maxi)
  
    # prall the factors of
    # numbers which divides the
    # maximum array elements
    for it in mpp:
        if (mpp[it] == maxi):
            print(it,end=" ")
      
# Driver Code
a = [12, 15, 27, 20, 40 ]
n = len(a)
printNumbers(a, n)
  
# This code is contributed by mohit kumar

chevron_right


Output:

5 2 3 4

Time Complexity: O(N * sqrt(max(array element)))



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.



Improved By : mohit kumar 29