Maximum Subarray Sum Excluding Certain Elements | Python3 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
import sys NEW_LINE def findMaxSubarraySum ( A , B ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( len ( B ) ) : NEW_LINE INDENT if B [ i ] not in m : NEW_LINE INDENT m [ B [ i ] ] = 0 NEW_LINE DEDENT m [ B [ i ] ] = 1 NEW_LINE DEDENT max_so_far = - sys . maxsize - 1 NEW_LINE currmax = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if ( currmax < 0 or ( A [ i ] in m and m [ A [ i ] ] == 1 ) ) : NEW_LINE INDENT currmax = 0 NEW_LINE continue NEW_LINE DEDENT currmax = max ( A [ i ] , A [ i ] + currmax ) NEW_LINE if ( max_so_far < currmax ) : NEW_LINE INDENT max_so_far = currmax NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 3 , 4 , 5 , - 4 , 6 ] NEW_LINE b = [ 1 , 8 , 5 ] NEW_LINE print ( findMaxSubarraySum ( a , b ) ) NEW_LINE DEDENT