Subarray with largest sum after excluding its maximum element | Function to find the maximum sum subarray by excluding the maximum element from the array ; Loop to store all the positive elements in the map ; Loop to iterating over the map and considering as the maximum element of the current including subarray ; Make the current element maximum ; Iterate through array and apply kadane 's algorithm ; Condition if current element is greater than mx then make the element - infinity ; Store the indices in some variable ; Driver Code ; Function Call
def maximumSumSubarray ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and arr [ i ] not in mp ) : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT first = 0 NEW_LINE last = 0 NEW_LINE ans = 0 NEW_LINE INF = 1e6 NEW_LINE for i in mp : NEW_LINE INDENT mx = i NEW_LINE curr = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( curr == 0 ) : NEW_LINE INDENT curr_start = j NEW_LINE DEDENT if arr [ j ] > mx : NEW_LINE INDENT val = - INF NEW_LINE DEDENT else : NEW_LINE INDENT val = arr [ j ] ; NEW_LINE DEDENT curr += val NEW_LINE if ( curr < 0 ) : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT if ( curr > ans ) : NEW_LINE INDENT ans = curr NEW_LINE first = curr_start NEW_LINE last = j NEW_LINE DEDENT DEDENT DEDENT print ( first + 1 , last + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , - 2 , 10 , - 1 , 4 ] NEW_LINE size = len ( arr ) NEW_LINE maximumSumSubarray ( arr , size ) NEW_LINE DEDENT