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 ; Given array arr [ ] ; Size of array ; Function Call
function splitArray ( arr , n , K ) { arr . sort ( ) ; let i = n - 1 ; let result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } document . write ( result ) ; } let arr = [ 1 , 3 , 1 , 5 , 1 , 3 ] ; let N = arr . length ; let K = 2 ; splitArray ( arr , N , K ) ;