Maximum circular subarray sum | The function returns maximum circular contiguous sum in a [ ] ; Corner Case ; Initialize sum variable which store total sum of the array . ; Initialize every variable with first value of array . ; Concept of Kadane 's Algorithm ; Kadane 's Algorithm to find Maximum subarray sum. ; Kadane 's Algorithm to find Minimum subarray sum. ; returning the maximum value ; Driver program to test maxCircularSum ( )
function maxCircularSum ( a , n ) { if ( n == 1 ) return a [ 0 ] ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } let curr_max = a [ 0 ] , max_so_far = a [ 0 ] , curr_min = a [ 0 ] , min_so_far = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { curr_max = Math . max ( curr_max + a [ i ] , a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; curr_min = Math . min ( curr_min + a [ i ] , a [ i ] ) ; min_so_far = Math . min ( min_so_far , curr_min ) ; } if ( min_so_far == sum ) return max_so_far ; return Math . max ( max_so_far , sum - min_so_far ) ; } let a = [ 11 , 10 , - 20 , 5 , - 3 , - 5 , 8 , - 13 , 10 ] ; let n = a . length ; document . write ( " " + maxCircularSum ( a , n ) ) ;