Find minimum value to assign all array elements so that array product becomes greater | C ++ program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val ld val = 0 ; where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE #define ld  long double NEW_LINE using namespace std ; ll findMinValue ( ll arr [ ] , ll n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) val += ( ld ) ( log ( ( ld ) ( arr [ i ] ) ) ) ; ll left = arr [ 0 ] , right = arr [ n - 1 ] + 1 ; ll ans ; while ( left <= right ) { ll mid = ( left + right ) / 2 ; ld temp = ( ld ) n * ( ld ) ( log ( ( ld ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else left = mid + 1 ; } return ans ; } int main ( ) { ll arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; ll n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinValue ( arr , n ) << endl ; return 0 ; }