Minimum value of X such that sum of arr [ i ] | Function to check if there exists an X that satisfies the given conditions ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver Code
function check ( a , b , k , n , x ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + Math . pow ( Math . max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } function max_element ( a ) { let maxi = Number . MIN_VALUE ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] > maxi ) { maxi = a [ i ] ; } } return maxi ; } function findMin ( a , b , n , k ) { let l = 0 , u = max_element ( a ) ; while ( l < u ) { let m = Math . floor ( ( l + u ) / 2 ) ; if ( check ( a , b , k , n , m ) ) { u = m ; } else { l = m + 1 ; } } return l ; } let arr = [ 2 , 1 , 4 , 3 , 5 ] ; let brr = [ 4 , 3 , 2 , 3 , 1 ] ; let K = 12 ; let N = arr . length ; document . write ( findMin ( arr , brr , N , K ) ) ;