Maximize Kth largest element after splitting the given Array at most C times | Java program for the above approach ; 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
import java . io . * ; class GFG { static double maxKth ( int arr [ ] , int N , int C , int K ) { if ( N + C < K ) { return - 1 ; } int iter = 300 ; double l = 0 , r = 1000000000.0 ; while ( iter -- > 0 ) { double mid = ( l + r ) * 0.5 ; double a = 0 ; double b = 0 ; for ( int i = 0 ; i < N ; i ++ ) { a += ( int ) ( ( double ) arr [ i ] / mid ) ; if ( ( double ) arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 } ; int K = 1 , C = 1 ; int N = arr . length ; System . out . println ( maxKth ( arr , N , C , K ) ) ; } }