Print the Maximum Subarray Sum | Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Given array arr [ ] ; Function call
def SubarrayWithMaxSum ( nums ) : NEW_LINE INDENT currMax = nums [ 0 ] NEW_LINE globalMax = nums [ 0 ] NEW_LINE for i in range ( 1 , len ( nums ) ) : NEW_LINE INDENT currMax = max ( nums [ i ] , nums [ i ] + currMax ) NEW_LINE if ( currMax > globalMax ) : NEW_LINE INDENT globalMax = currMax NEW_LINE endIndex = i NEW_LINE DEDENT DEDENT startIndex = endIndex NEW_LINE while ( startIndex >= 0 ) : NEW_LINE INDENT globalMax -= nums [ startIndex ] NEW_LINE if ( globalMax == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT startIndex -= 1 NEW_LINE DEDENT for i in range ( startIndex , endIndex + 1 ) : NEW_LINE INDENT print ( nums [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ - 2 , - 5 , 6 , - 2 , - 3 , 1 , 5 , - 6 ] NEW_LINE SubarrayWithMaxSum ( arr ) NEW_LINE