Maximize Array sum by swapping at most K elements with another array | Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code
function maximumSum ( a , b , k , n ) { let i , j ; a . sort ( ) ; b . sort ( ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { let temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } let sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; document . write ( sum ) ; } let K = 1 ; let A = [ 2 , 3 , 4 ] ; let B = [ 6 , 8 , 5 ] ; let N = A . length ; maximumSum ( A , B , K , N ) ;