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++
// 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; } |
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 |
20 8
Recommended Posts:
- Find numbers with n-divisors in a given range
- Find numbers with K odd divisors in a given range
- Find the number of divisors of all numbers in the range [1, n]
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Find sum of divisors of all the divisors of a natural number
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Numbers with exactly 3 divisors
- Sum of common divisors of two numbers A and B
- Common Divisors of Two Numbers
- Check if sum of divisors of two numbers are same
- Number of divisors of product of N numbers
- Sum of all prime divisors of all the numbers in range L-R
- C++ Program for Common Divisors of Two Numbers
- Sum of all proper divisors of natural numbers in an array
- Java Program for Common Divisors of Two Numbers
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 : AnkitRai01



