Construct an array from its pair | C ++ implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated array ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void constructArr ( int pair [ ] , int n ) { int size = ( 1 + ( int ) sqrt ( 1 + 8 * n ) ) / 2 ; int arr [ size ] ; arr [ 0 ] = sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } int main ( ) { int pair [ ] = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = sizeof ( pair ) / sizeof ( int ) ; constructArr ( pair , n ) ; return 0 ; }