Minimum value of X such that sum of arr [ i ] | C # 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
using System ; public class GFG { static bool 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 ; } for ( int i = 0 ; i < a . Length ; ++ ) { = + ( int ) Math . Pow ( Math . Max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } static int ( 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 ( String [ ] args ) { int n = 5 ; int k = 12 ; int [ ] a = { 2 , 1 , 4 , 3 , 5 } ; int [ ] b = { 4 , 3 , 2 , 3 , 1 } ; Console . WriteLine ( findMin ( a , b , n , k ) ) ; } }