Maximum subarray sum in an array created after repeated concatenation | Returns sum of maximum sum subarray created after concatenating a [ 0. . n - 1 ] k times . ; This is where it differs from Kadane 's algorithm.  We use modular arithmetic  to find next element. ; Driver Code
< ? php function maxSubArraySumRepeated ( $ a , $ n , $ k ) { $ INT_MIN = 0 ; $ max_so_far = $ INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ n * $ k ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i % $ n ] ; 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 ; } $ a = array ( 10 , 20 , -30 , -1 ) ; $ n = sizeof ( $ a ) ; $ k = 3 ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , maxSubArraySumRepeated ( $ a , $ n , $ k ) ; ? >