Split array into K subsets to maximize sum of their second largest elements | Java program for the above approach ; 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 ; Drive Code ; Given array arr [ ] ; Size of array ; Function Call
import java . io . * ; import java . util . Arrays ; class GFG { static void splitArray ( int arr [ ] , int n , int K ) { Arrays . sort ( arr ) ; int i = n - 1 ; int result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } System . out . print ( result ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 5 , 1 , 3 } ; int N = arr . length ; int K = 2 ; splitArray ( arr , N , K ) ; } }