Find minimum value to assign all array elements so that array product becomes greater | Javascript program to find minimum value that can be assigned to along 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 let val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
function findMinValue ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { val += ( Math . log ( ( arr [ i ] ) ) ) ; } let left = arr [ 0 ] , right = arr [ n - 1 ] ; let ans = 0 ; while ( left <= right ) { let mid = Math . floor ( ( left + right ) / 2 ) ; let temp = n * ( Math . log ( ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } let arr = [ 4 , 2 , 1 , 10 , 6 ] ; let n = arr . length ; document . write ( findMinValue ( arr , n ) ) ;