Minimise the maximum element of N subarrays of size K | Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
def minDays ( arr , n , k ) : NEW_LINE INDENT l = len ( arr ) NEW_LINE left = 1 NEW_LINE right = 1e9 NEW_LINE if ( n * k > l ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( left < right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE cnt = 0 NEW_LINE product = 0 NEW_LINE for j in range ( l ) : NEW_LINE INDENT if ( arr [ j ] > mid ) : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE if ( cnt >= k ) : NEW_LINE INDENT product += 1 NEW_LINE cnt = 0 NEW_LINE DEDENT DEDENT DEDENT if ( product < n ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid NEW_LINE DEDENT DEDENT return left NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 10 , 3 , 10 , 2 ] NEW_LINE n = 3 NEW_LINE k = 1 NEW_LINE print ( int ( minDays ( arr , n , k ) ) ) NEW_LINE DEDENT