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
def maxKth ( arr , N , C , K ) : NEW_LINE INDENT if ( N + C < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT iter = 300 NEW_LINE l = 0 NEW_LINE r = 1000000000.0 NEW_LINE while ( iter ) : NEW_LINE INDENT iter = iter - 1 NEW_LINE mid = ( l + r ) * 0.5 NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT a += arr [ i ] // mid NEW_LINE if ( arr [ i ] >= mid ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT DEDENT if ( a >= K and b + C >= K ) : NEW_LINE INDENT l = mid NEW_LINE DEDENT else : NEW_LINE INDENT r = mid NEW_LINE DEDENT DEDENT return int ( l ) NEW_LINE DEDENT arr = [ 5 , 8 ] NEW_LINE K = 1 NEW_LINE C = 1 NEW_LINE N = len ( arr ) NEW_LINE print ( maxKth ( arr , N , C , K ) ) NEW_LINE