Minimum value of X such that sum of arr [ i ] | Java program for the above approach ; Function to check if it is possible to get desired result ; 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
import java . io . * ; class GFG { static boolean check ( int a [ ] , int b [ ] , int k , int x ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { sum = sum + ( int ) Math . pow ( Math . max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } static int findMin ( int a [ ] , int b [ ] , int n , int k ) { int l = 0 , u = ( int ) 1e9 ; while ( l < u ) { int m = ( l + u ) / 2 ; if ( check ( a , b , k , m ) ) u = m ; else l = m + 1 ; } return l ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 12 ; int a [ ] = { 2 , 1 , 4 , 3 , 5 } ; int b [ ] = { 4 , 3 , 2 , 3 , 1 } ; System . out . println ( findMin ( a , b , n , k ) ) ; } }