Find two numbers whose divisors are given in a random order

Given an array of N numbers which has all divisors of two numbers in any order. The task is to find the two numbers whose divisors are given in the array.

Examples:

Input: a[] = {10, 2, 8, 1, 2, 4, 1, 20, 4, 5}
Output: 20 8
The divisors of 20 and 8 are given in the array.

Input: a[] = {2, 2, 1, 1}
Output: 1 2

Approach: The following steps are followed to solve the above problem:

  • Find the maximum element in the array, which is one of the two numbers.
  • Keep a frequency array which counts the array element’s occurrence.
  • Decrease the count of all divisors of the maximum element in the frequency array.
  • The next max element will be the second number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the two numbers
// when divisors are given in a random order
#include <bits/stdc++.h>
using namespace std;
  
// Function to get the two numbers
void getNumbers(int a[], int n)
{
  
    // Mark the frequency of all elements
    unordered_map<int, int> freq;
    for (int i = 0; i < n; i++)
        freq[a[i]]++;
  
    // Get the first maximum element
    int maxi1 = *max_element(a, a+n);
  
    // Decrease the frequency of all divisors
    // of the maximum number that are present
    // int a[]
    for (int i = 1; i * i <= maxi1; i++) {
  
        if (maxi1 % i == 0 && 
            freq.find(i) != freq.end() && 
            freq[i] != 0) {
            freq[i]--;
  
            if (i != (maxi1 / i) && 
               freq.find(maxi1 / i) != freq.end() && 
               freq[maxi1 / i] != 0)
                freq[maxi1 / i]--;
        }
    }
  
    // The second number is the largest number
    // present in remaining numbers.
    int maxi2 = -1;
    for (int i = 0; i < n; i++) {
        if (freq[a[i]] != 0)
            maxi2 = max(maxi2, a[i]);
    }
  
    cout << maxi1 << " " << maxi2;
}
  
// Driver Code
int main()
{
    int a[] = { 10, 2, 8, 1, 2, 4, 1, 20, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    getNumbers(a, n);
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the two numbers 
# when divisors are given in a random order 
  
# from math lib import sqrt method
from math import sqrt
  
# Function to get the two numbers 
def getNumbers(a, n) : 
  
    # Mark the frequency of all elements 
    freq = {}; 
    for i in range(n) :
        if a[i] not in freq.keys() :
            freq[a[i]] = 0
              
        freq[a[i]] += 1
  
    # Get the first maximum element 
    maxi1 = max(a)
  
    # Decrease the frequency of all divisors 
    # of the maximum number that are present 
    # int a[] 
    for i in range(1, int(sqrt(maxi1)) + 1) :
  
        if (maxi1 % i == 0 and 
            freq[i] in freq.keys() and 
            freq[i] != 0) : 
            freq[i] -= 1
  
            if (i != (maxi1 // i) and 
                freq[maxi1 // i] in freq.keys() and 
                freq[maxi1 // i] != 0) : 
                freq[maxi1 // i] -= 1
      
    # The second number is the largest number 
    # present in remaining numbers. 
    maxi2 = -1
    for i in range(n) :
        if (freq[a[i]] != 0) :
            maxi2 = max(maxi2, a[i]) 
  
    print(maxi1, maxi2) 
  
# Driver Code 
if __name__ == "__main__" :
      
    a = [ 10, 2, 8, 1, 2, 4, 1, 20, 4, 5
    n = len(a)
    getNumbers(a, n)
      
# This code is contributed by Ryuga

chevron_right


Output:

20 8


My Personal Notes arrow_drop_up