Maximum sum subarray after altering the array | Function that returns true if all the array element are <= 0 ; If any element is non - negative ; Function to return the vector representing the right to left Kadane array as described in the approach ; Function to return the prefix_sum vector ; Function to return the maximum sum subarray ; Function to get the maximum sum subarray in the modified array ; kadane_r_to_l [ i ] will store the maximum subarray sum for thre subarray arr [ i ... N - 1 ] ; Get the prefix sum array ; To get max_prefix_sum_at_any_index ; Summation of both gives the maximum subarray sum after applying the operation ; Function to return the maximum subarray sum after performing the given operation at most once ; If all element are negative then return the maximum element ; Maximum subarray sum without performing any operation ; Maximum subarray sum after performing the operations of first type ; Reversing the array to use the same existing function for operations of the second type ; Driver code
function areAllNegative ( arr ) { let n = arr . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; } return true ; } function getRightToLeftKadane ( arr ) { let max_so_far = 0 , max_ending_here = 0 ; let size = arr . length ; let new_arr = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) new_arr [ i ] = arr [ i ] ; for ( let i = size - 1 ; i >= 0 ; i -- ) { max_ending_here = max_ending_here + new_arr [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; new_arr [ i ] = max_so_far ; } return new_arr ; } function getPrefixSum ( arr ) { let n = arr . length ; let new_arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) new_arr [ i ] = arr [ i ] ; for ( let i = 1 ; i < n ; i ++ ) new_arr [ i ] = new_arr [ i - 1 ] + new_arr [ i ] ; return new_arr ; } function maxSumSubArr ( a ) { let max_so_far = 0 , max_ending_here = 0 ; let n = a . length ; for ( let i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } function maxSumSubWithOp ( arr ) { let kadane_r_to_l = getRightToLeftKadane ( arr ) ; let size = arr . length ; let prefixSum = getPrefixSum ( arr ) ; for ( let i = 1 ; i < size ; i ++ ) { prefixSum [ i ] = Math . max ( prefixSum [ i - 1 ] , prefixSum [ i ] ) ; } let max_subarray_sum = 0 ; for ( let i = 0 ; i < size - 1 ; i ++ ) { max_subarray_sum = Math . max ( max_subarray_sum , prefixSum [ i ] + kadane_r_to_l [ i + 1 ] ) ; } return max_subarray_sum ; } function maxSum ( arr , size ) { if ( areAllNegative ( arr ) ) { let mx = - 1000000000 ; for ( let i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > mx ) mx = arr [ i ] ; } return mx ; } let resSum = maxSumSubArr ( arr ) ; resSum = Math . max ( resSum , maxSumSubWithOp ( arr ) ) ; let reverse_arr = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) reverse_arr [ size - 1 - i ] = arr [ i ] ; resSum = Math . max ( resSum , maxSumSubWithOp ( reverse_arr ) ) ; return resSum ; } let arr = [ - 9 , 21 , 24 , 24 , - 51 , - 6 , 17 , - 42 , - 39 , 33 ] ; let size = arr . length ; document . write ( maxSum ( arr , size ) ) ;