Maximum Subarray Sum Excluding Certain Elements | C # Program implementation of the above idea ; Function to calculate the max sum of contigous subarray of B whose elements are not present in A ; mark all the elements present in B ; initialize max_so_far with INT_MIN ; traverse the array A ; if current max is greater than max so far then update max so far ; Driver code . ; Function call
using System ; using System . Collections . Generic ; class GFG { static int findMaxSubarraySum ( int [ ] A , int [ ] B ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < B . Length ; i ++ ) { m [ B [ i ] ] = 1 ; } int max_so_far = Int32 . MinValue ; int currmax = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( currmax < 0 || ( m . ContainsKey ( A [ i ] ) && m [ A [ i ] ] == 1 ) ) { currmax = 0 ; continue ; } currmax = Math . Max ( A [ i ] , A [ i ] + currmax ) ; if ( max_so_far < currmax ) { max_so_far = currmax ; } } return max_so_far ; } static void Main ( ) { int [ ] a = { 3 , 4 , 5 , - 4 , 6 } ; int [ ] b = { 1 , 8 , 5 } ; Console . WriteLine ( findMaxSubarraySum ( a , b ) ) ; } }