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) = 5Input: 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:
- Sort the array in decreasing order.
- Highest element will always be one of the original numbers. Keep that number and remove it from the array.
- 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 <bits/stdc++.h> 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
<?php // PHP implementation of the approach function gcd( $a , $b ) { return ( $a % $b ) ? gcd( $b , $a % $b ) : $b ; } // Utility function to print // the contents of an array function printArr( $arr , $n ) { for ( $i = 0; $i < $n ; $i ++) echo $arr [ $i ], " " ; } // Function to find the required numbers function findNumbers( $arr , $n ) { // Sort array in decreasing order rsort( $arr ); $freq = array_fill (0, $arr [0] + 1, 0); // Count frequency of each element for ( $i = 0; $i < $n ; $i ++) $freq [ $arr [ $i ]]++; // Size of the resultant array $size = floor (sqrt( $n )); $brr = array_fill (0, $size , 0); $l = 0; for ( $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 ( $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
<script> // Javascript implementation for above approach // Utility function to print // the contents of an array function printArr(arr, n) { for (let i = 0; i < n; i++) { document.write(arr[i] + " " ); } } // Function to find the required numbers function findNumbers(arr, n) { // Sort array in decreasing order arr.sort( function (a, b){ return a - b}); reverse(arr); let freq = new Array(arr[0] + 1); freq.fill(0); // Count frequency of each element for (let i = 0; i < n; i++) { freq[arr[i]]++; } // Size of the resultant array let size = parseInt(Math.sqrt(n), 10); let brr = new Array(size); brr.fill(0); let x, l = 0; for (let 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 (let 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 function reverse(input) { let last = input.length - 1; let middle = parseInt(input.length / 2, 10); for (let i = 0; i <= middle; i++) { let temp = input[i]; input[i] = input[last - i]; input[last - i] = temp; } } function __gcd(a, b) { if (b == 0) { return a; } return __gcd(b, a % b); } // Driver code let 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]; let n = arr.length; findNumbers(arr, n); // This code is contributed by divyeshrabadiya07 </script> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!