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
function maximumSumSubarray ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 ) ; } var first = 0 ; var last = 0 ; var ans = 0 ; var INF = 1000000 ; mp . forEach ( ( value , key ) => { var mx = key ; var curr = 0 ; var curr_start ; for ( var j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; var val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } ) ; document . write ( first + 1 + " " + ( last + 1 ) ) ; } var arr = [ 5 , - 2 , 10 , - 1 , 4 ] ; var size = arr . length ; maximumSumSubarray ( arr , size ) ;