Skip to content
Related Articles

Related Articles

Improve Article

Find two numbers whose divisors are given in a random order

  • Difficulty Level : Easy
  • Last Updated : 05 Jul, 2021

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




// 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;
}

Java




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

Python3




# 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

C#




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

Javascript




<script>
// Javascript program to find the two numbers
// when divisors are given in a random order
  
   
// Function to get the two numbers
function getNumbers(a, n)
{
   
    // Mark the frequency of all elements
    var freq = new Map();
    for (var i = 0; i < n; i++)
        if(freq.has(a[i])){
            freq.set(a[i], freq.get(a[i])+1);
        }
        else{
            freq.set(a[i], 1);
        }
   
    // Get the first maximum element
    var maxi1 = a.reduce((a1, b1)=>Math.max(a1,b1));
   
    // Decrease the frequency of all divisors
    // of the maximum number that are present
    // int []a
    for (var i = 1; i * i <= maxi1; i++) {
   
        if (maxi1 % i == 0 &&
            (freq.has(i)&&
            freq.get(i)!= 0)) {
            freq.set(i, freq.get(i)-1);
   
            if (i != parseInt(maxi1 / i) &&
               freq.has(parseInt(maxi1 / i)) &&
               freq.get(parseInt(maxi1 / i))!= 0)
               freq.set(parseInt(maxi1 / i), freq.get(parseInt(maxi1 / i))-1);
        }
    }
   
    // The second number is the largest number
    // present in remaining numbers.
    var maxi2 = -1;
    for (var i = 0; i < n; i++) {
        if (freq.get(a[i])!=0)
            maxi2 = Math.max(maxi2, a[i]);
    }
   
    document.write(maxi1+ " " +  maxi2);
}
   
// Driver Code
var a = [10, 2, 8, 1, 2, 4, 1, 20, 4, 5];
var n = a.length;
getNumbers(a, n);
 
// This code is contributed by rutvik_56.
</script>
Output: 
20 8

 

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Competitive Programming Live  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :