A product array puzzle | Set 2 ( O ( 1 ) Space ) | C ++ program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index antilog to find original product value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define EPS  1e-9 NEW_LINE void productPuzzle ( int a [ ] , int n ) { long double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( long double ) log10 ( a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) cout << ( int ) ( EPS + pow ( ( long double ) 10.00 , sum - log10 ( a [ i ] ) ) ) << " ▁ " ; } int main ( ) { int a [ ] = { 10 , 3 , 5 , 6 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " The ▁ product ▁ array ▁ is : ▁ STRNEWLINE " ; productPuzzle ( a , n ) ; return 0 ; }