Maximize maximum possible subarray sum of an array by swapping with elements from another array | C # program for the above approach ; Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver Code ; Given arrays and respective lengths ; Calculate maximum subarray sum
using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] arr , int [ ] brr , int N , int K ) { List < int > crr = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { crr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . Add ( brr [ i ] ) ; } crr . Sort ( ) ; crr . Reverse ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } Console . WriteLine ( sum ) ; } static void Main ( ) { int [ ] arr = { 7 , 2 , - 1 , 4 , 5 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 , 3 , 2 } ; int K = brr . Length ; maxSum ( arr , brr , N , K ) ; } }