Maximum sum of all elements of array after performing given operations | Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } function maxSum ( $ a , $ n ) { $ S = 0 ; $ S1 = maxSubArraySum ( $ a , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ S += $ a [ $ i ] ; return ( 2 * $ S1 - $ S ) ; } $ a = array ( -35 , 32 , -24 , 0 , 27 , -10 , 0 , -19 ) ; $ n = sizeof ( $ a ) ; echo ( maxSum ( $ a , $ n ) ) ;