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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the two numbers
// when divisors are given in a random order
  
import java.util.*;
  
class GFG{
   
// Function to get the two numbers
static void getNumbers(int a[], int n)
{
   
    // Mark the frequency of all elements
    HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
    for (int i = 0; i < n; i++)
        if(freq.containsKey(a[i])){
            freq.put(a[i], freq.get(a[i])+1);
        }
        else{
            freq.put(a[i], 1);
        }
   
    // Get the first maximum element
    int maxi1 = Arrays.stream(a).max().getAsInt();
   
    // 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.containsKey(i)&& 
            freq.get(i)!= 0) {
            freq.put(i, freq.get(i)- 1);
   
            if (i != (maxi1 / i) && 
               freq.containsKey(maxi1 / i) && 
               freq.get(maxi1 / i)!= 0)
                freq.put(maxi1 / i, freq.get(maxi1 / i) - 1);
        }
    }
   
    // The second number is the largest number
    // present in remaining numbers.
    int maxi2 = -1;
    for (int i = 0; i < n; i++) {
        if (freq.get(a[i]) != 0)
            maxi2 = Math.max(maxi2, a[i]);
    }
   
    System.out.print(maxi1+ " " +  maxi2);
}
   
// Driver Code
public static void main(String[] args)
{
    int a[] = { 10, 2, 8, 1, 2, 4, 1, 20, 4, 5 };
    int n = a.length;
    getNumbers(a, n);
}
}
  
// This code contributed by Rajput-Ji

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


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
   
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG{
    
// Function to get the two numbers
static void getNumbers(int []a, int n)
{
    
    // Mark the frequency of all elements
    Dictionary<int,int> freq = new Dictionary<int,int>();
    for (int i = 0; i < n; i++)
        if(freq.ContainsKey(a[i])){
            freq[a[i]] = freq[a[i]]+1;
        }
        else{
            freq.Add(a[i], 1);
        }
    
    // Get the first maximum element
    int maxi1 = a.Max();
    
    // 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.ContainsKey(i)&& 
            freq[i]!= 0) {
            freq[i] = freq[i]- 1;
    
            if (i != (maxi1 / i) && 
               freq.ContainsKey(maxi1 / i) && 
               freq[maxi1 / i]!= 0)
                freq[maxi1 / i] = freq[maxi1 / i] - 1;
        }
    }
    
    // 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 = Math.Max(maxi2, a[i]);
    }
    
    Console.Write(maxi1+ " " +  maxi2);
}
    
// Driver Code
public static void Main(String[] args)
{
    int []a = { 10, 2, 8, 1, 2, 4, 1, 20, 4, 5 };
    int n = a.Length;
    getNumbers(a, n);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

20 8

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.