Split array into equal length subsets with maximum sum of Kth largest element of each subset | Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver code
def maximumKthLargestsumPart ( arr , N , M , K ) : NEW_LINE INDENT maxSum = 0 NEW_LINE if ( N % M != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT sz = ( N / M ) NEW_LINE if ( K > sz ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE for i in range ( 0 , N // 2 ) : NEW_LINE INDENT t = arr [ i ] NEW_LINE arr [ i ] = arr [ N - i - 1 ] NEW_LINE arr [ N - i - 1 ] = t NEW_LINE DEDENT for i in range ( 1 , M + 1 ) : NEW_LINE INDENT maxSum += arr [ i * K - 1 ] NEW_LINE DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE M = 2 NEW_LINE K = 1 NEW_LINE N = len ( arr ) NEW_LINE print ( maximumKthLargestsumPart ( arr , N , M , K ) ) NEW_LINE DEDENT