Open In App

# Find original numbers from gcd() every pair

Given an array arr[] containing GCD of every possible pair of elements of another array. The task is to find the original numbers which are used to calculate the GCD array. For example, if original numbers are {4, 6, 8} then the given array will be {4, 2, 4, 2, 6, 2, 4, 2, 8}.

Examples:

Input: arr[] = {5, 1, 1, 12}
Output: 12 5
gcd(12, 12) = 12
gcd(12, 5) = 1
gcd(5, 12) = 1
gcd(5, 5) = 5

Input: arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2}
Output: 12 10 7 5 1

Approach:

1. Sort the array in decreasing order.
2. Highest element will always be one of the original numbers. Keep that number and remove it from the array.
3. Compute GCD of the element taken in the previous step with the current element starting from the greatest and discard the GCD value from the given array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Utility function to print``// the contents of an array``void` `printArr(``int` `arr[], ``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++)``        ``cout << arr[i] << ``" "``;``}` `// Function to find the required numbers``void` `findNumbers(``int` `arr[], ``int` `n)``{` `    ``// Sort array in decreasing order``    ``sort(arr, arr + n, greater<``int``>());` `    ``int` `freq[arr[0] + 1] = { 0 };` `    ``// Count frequency of each element``    ``for` `(``int` `i = 0; i < n; i++)``        ``freq[arr[i]]++;` `    ``// Size of the resultant array``    ``int` `size = ``sqrt``(n);``    ``int` `brr[size] = { 0 }, x, l = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(freq[arr[i]] > 0) {` `            ``// Store the highest element in``            ``// the resultant array``            ``brr[l] = arr[i];` `            ``// Decrement the frequency of that element``            ``freq[brr[l]]--;``            ``l++;``            ``for` `(``int` `j = 0; j < l; j++) {``                ``if` `(i != j) {` `                    ``// Compute GCD``                    ``x = __gcd(arr[i], brr[j]);` `                    ``// Decrement GCD value by 2``                    ``freq[x] -= 2;``                ``}``            ``}``        ``}``    ``}` `    ``printArr(brr, size);``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,``                  ``1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``findNumbers(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.Arrays;` `class` `GFG``{` `    ``// Utility function to print``    ``// the contents of an array``    ``static` `void` `printArr(``int` `arr[], ``int` `n)``    ``{``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``System.out.print(arr[i] + ``" "``);``        ``}``    ``}` `    ``// Function to find the required numbers``    ``static` `void` `findNumbers(``int` `arr[], ``int` `n)``    ``{` `        ``// Sort array in decreasing order``        ``Arrays.sort(arr);``        ``reverse(arr);``        ``int` `freq[] = ``new` `int``[arr[``0``] + ``1``];` `        ``// Count frequency of each element``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``freq[arr[i]]++;``        ``}` `        ``// Size of the resultant array``        ``int` `size = (``int``) Math.sqrt(n);``        ``int` `brr[] = ``new` `int``[size], x, l = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``if` `(freq[arr[i]] > ``0` `&& l < size)``            ``{` `                ``// Store the highest element in``                ``// the resultant array``                ``brr[l] = arr[i];` `                ``// Decrement the frequency of that element``                ``freq[brr[l]]--;``                ``l++;``                ``for` `(``int` `j = ``0``; j < l; j++)``                ``{``                    ``if` `(i != j)``                    ``{` `                        ``// Compute GCD``                        ``x = __gcd(arr[i], brr[j]);` `                        ``// Decrement GCD value by 2``                        ``freq[x] -= ``2``;``                    ``}``                ``}``            ``}``        ``}` `        ``printArr(brr, size);``    ``}``    ` `    ``// reverse array``    ``public` `static` `void` `reverse(``int``[] input)``    ``{``        ``int` `last = input.length - ``1``;``        ``int` `middle = input.length / ``2``;``        ``for` `(``int` `i = ``0``; i <= middle; i++)``        ``{``            ``int` `temp = input[i];``            ``input[i] = input[last - i];``            ``input[last - i] = temp;``        ``}``    ``}` `    ``static` `int` `__gcd(``int` `a, ``int` `b)``    ``{``        ``if` `(b == ``0``)``        ``{``            ``return` `a;``        ``}``        ``return` `__gcd(b, a % b);` `    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = {``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``,``            ``1``, ``1``, ``1``, ``5``, ``5``, ``5``, ``7``, ``10``, ``12``, ``2``, ``2``};``        ``int` `n = arr.length;``        ``findNumbers(arr, n);``    ``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python 3 implementation of the approach``from` `math ``import` `sqrt, gcd` `# Utility function to print``# the contents of an array``def` `printArr(arr, n):``    ``for` `i ``in` `range``(n):``        ``print``(arr[i], end ``=` `" "``)` `# Function to find the required numbers``def` `findNumbers(arr, n):``    ` `    ``# Sort array in decreasing order``    ``arr.sort(reverse ``=` `True``)` `    ``freq ``=` `[``0` `for` `i ``in` `range``(arr[``0``] ``+` `1``)]` `    ``# Count frequency of each element``    ``for` `i ``in` `range``(n):``        ``freq[arr[i]] ``+``=` `1` `    ``# Size of the resultant array``    ``size ``=` `int``(sqrt(n))``    ``brr ``=` `[``0` `for` `i ``in` `range``(``len``(arr))]``    ``l ``=` `0` `    ``for` `i ``in` `range``(n):``        ``if` `(freq[arr[i]] > ``0``):``            ` `            ``# Store the highest element in``            ``# the resultant array``            ``brr[l] ``=` `arr[i]` `            ``# Decrement the frequency of that element``            ``freq[brr[l]] ``-``=` `1``            ``l ``+``=` `1``            ``for` `j ``in` `range``(l):``                ``if` `(i !``=` `j):``                    ` `                    ``# Compute GCD``                    ``x ``=` `gcd(arr[i], brr[j])` `                    ``# Decrement GCD value by 2``                    ``freq[x] ``-``=` `2` `    ``printArr(brr, size)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``,``           ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``,``           ``1``, ``5``, ``5``, ``5``, ``7``, ``10``, ``12``, ``2``, ``2``]``    ``n ``=` `len``(arr)``    ``findNumbers(arr, n)``    ` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation for above approach``using` `System;``    ` `class` `GFG``{` `    ``// Utility function to print``    ``// the contents of an array``    ``static` `void` `printArr(``int` `[]arr, ``int` `n)``    ``{``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``Console.Write(arr[i] + ``" "``);``        ``}``    ``}` `    ``// Function to find the required numbers``    ``static` `void` `findNumbers(``int` `[]arr, ``int` `n)``    ``{` `        ``// Sort array in decreasing order``        ``Array.Sort(arr);``        ``reverse(arr);``        ``int` `[]freq = ``new` `int``[arr[0] + 1];` `        ``// Count frequency of each element``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``freq[arr[i]]++;``        ``}` `        ``// Size of the resultant array``        ``int` `size = (``int``) Math.Sqrt(n);``        ``int` `[]brr = ``new` `int``[size];``int` `x, l = 0;` `        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``if` `(freq[arr[i]] > 0 && l < size)``            ``{` `                ``// Store the highest element in``                ``// the resultant array``                ``brr[l] = arr[i];` `                ``// Decrement the frequency of that element``                ``freq[brr[l]]--;``                ``l++;``                ``for` `(``int` `j = 0; j < l; j++)``                ``{``                    ``if` `(i != j)``                    ``{` `                        ``// Compute GCD``                        ``x = __gcd(arr[i], brr[j]);` `                        ``// Decrement GCD value by 2``                        ``freq[x] -= 2;``                    ``}``                ``}``            ``}``        ``}` `        ``printArr(brr, size);``    ``}``    ` `    ``// reverse array``    ``public` `static` `void` `reverse(``int` `[]input)``    ``{``        ``int` `last = input.Length - 1;``        ``int` `middle = input.Length / 2;``        ``for` `(``int` `i = 0; i <= middle; i++)``        ``{``            ``int` `temp = input[i];``            ``input[i] = input[last - i];``            ``input[last - i] = temp;``        ``}``    ``}` `    ``static` `int` `__gcd(``int` `a, ``int` `b)``    ``{``        ``if` `(b == 0)``        ``{``            ``return` `a;``        ``}``        ``return` `__gcd(b, a % b);` `    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `[]arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,``            ``1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2};``        ``int` `n = arr.Length;``        ``findNumbers(arr, n);``    ``}``}` `/* This code contributed by PrinciRaj1992 */`

## PHP

 ` 0)``        ``{` `            ``// Store the highest element in``            ``// the resultant array``            ``\$brr``[``\$l``] = ``\$arr``[``\$i``];` `            ``// Decrement the frequency of that element``            ``\$freq``[``\$brr``[``\$l``]]--;``            ``\$l``++;``            ``for` `(``\$j` `= 0; ``\$j` `< ``\$l``; ``\$j``++)``            ``{``                ``if` `(``\$i` `!= ``\$j``)``                ``{` `                    ``// Compute GCD``                    ``\$x` `= gcd(``\$arr``[``\$i``], ``\$brr``[``\$j``]);``                    ` `                    ``// Decrement GCD value by 2``                    ``\$freq``[``\$x``] -= 2;``                ``}``            ``}``        ``}``    ``}` `    ``printArr(``\$brr``, ``\$size``);``}` `// Driver code``\$arr` `= ``array``(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  ``             ``1, 1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2 );``\$n` `= ``count``(``\$arr``) ;``findNumbers(``\$arr``, ``\$n``);` `// This code is contributed by Ryuga``?>`

## Javascript

 ``

Output:

`12 10 7 5 1`

Time Complexity: O(n2)
Auxiliary Space: O(?n+k) where n is the size of array and k is the maximum element of the array.