Maximize Kth largest element after splitting the given Array at most C times | Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code
function maxKth ( arr , N , C , K ) { if ( N + C < K ) { return - 1 ; } let iter = 300 ; let l = 0 , r = 1000000000.0 ; while ( iter -- ) { let mid = ( l + r ) * 0.5 ; let a = 0 ; let b = 0 ; for ( let i = 0 ; i < N ; i ++ ) { a += Math . floor ( arr [ i ] / mid ) ; if ( arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } let arr = [ 5 , 8 ] ; let K = 1 , C = 1 ; let N = arr . length ; document . write ( maxKth ( arr , N , C , K ) ) ;