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
def check ( a , b , k , n , x ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + pow ( max ( a [ i ] - x , 0 ) , b [ i ] ) NEW_LINE DEDENT if ( sum <= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def findMin ( a , b , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE u = max ( a ) NEW_LINE while ( l < u ) : NEW_LINE INDENT m = ( l + u ) // 2 NEW_LINE if ( check ( a , b , k , n , m ) ) : NEW_LINE INDENT u = m NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 3 , 5 ] NEW_LINE brr = [ 4 , 3 , 2 , 3 , 1 ] NEW_LINE K = 12 NEW_LINE N = len ( arr ) NEW_LINE print ( findMin ( arr , brr , N , K ) ) NEW_LINE DEDENT