Find the maximum sum of Plus shape pattern in a 2 | JavaScript program to find the maximum value of a + shaped pattern in 2 - D array ; Function to return maximum Plus value ; Initializing answer with the minimum value ; Initializing all four arrays ; Initializing left and up array . ; Initializing right and down array . ; calculating value of maximum Plus ( + ) sign ; Driver code ; Function call to find maximum value
let N = 100 ; let n = 3 , m = 4 ; function maxPlus ( arr ) { let ans = 0 ; let left = new Array ( N ) ; let right = new Array ( N ) ; let up = new Array ( N ) ; let down = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { left [ i ] = new Array ( N ) ; right [ i ] = new Array ( N ) ; up [ i ] = new Array ( N ) ; down [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { left [ i ] [ j ] = 0 ; right [ i ] [ j ] = 0 ; up [ i ] [ j ] = 0 ; down [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { left [ i ] [ j ] = Math . max ( 0 , ( ( j != 0 ) ? left [ i ] [ j - 1 ] : 0 ) ) + arr [ i ] [ j ] ; up [ i ] [ j ] = Math . max ( 0 , ( ( i != 0 ) ? up [ i - 1 ] [ j ] : 0 ) ) + arr [ i ] [ j ] ; } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { right [ i ] [ j ] = Math . max ( 0 , ( j + 1 == m ? 0 : right [ i ] [ j + 1 ] ) ) + arr [ i ] [ j ] ; down [ i ] [ j ] = Math . max ( 0 , ( i + 1 == n ? 0 : down [ i + 1 ] [ j ] ) ) + arr [ i ] [ j ] ; } } for ( let i = 1 ; i < n - 1 ; ++ i ) for ( let j = 1 ; j < m - 1 ; ++ j ) { ans = Math . max ( ans , up [ i - 1 ] [ j ] + down [ i + 1 ] [ j ] + left [ i ] [ j - 1 ] + right [ i ] [ j + 1 ] + arr [ i ] [ j ] ) ; } return ans ; } let arr = [ [ 1 , 1 , 1 , 1 ] , [ - 6 , 1 , 1 , - 4 ] , [ 1 , 1 , 1 , 1 ] ] ; document . write ( maxPlus ( arr ) ) ;