Split array into K subsets to maximize sum of their second largest elements | Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def splitArray ( arr , n , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = n - 1 NEW_LINE result = 0 NEW_LINE while ( K > 0 ) : NEW_LINE INDENT result += arr [ i - 1 ] NEW_LINE i -= 2 NEW_LINE K -= 1 NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 1 , 5 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE splitArray ( arr , N , K ) NEW_LINE DEDENT