Find the maximum sum of Plus shape pattern in a 2 | Java 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
class GFG { public static int N = 100 ; public static int n = 3 , m = 4 ; public static int maxPlus ( int [ ] [ ] arr ) { int ans = Integer . MIN_VALUE ; int [ ] [ ] left = new int [ N ] [ N ] ; int [ ] [ ] right = new int [ N ] [ N ] ; int [ ] [ ] up = new int [ N ] [ N ] ; int [ ] [ ] down = new int [ N ] [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int 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 ( int i = 0 ; i < n ; i ++ ) { for ( int 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 ( int i = 1 ; i < n - 1 ; ++ i ) for ( int 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 ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = new int [ ] [ ] { { 1 , 1 , 1 , 1 } , { - 6 , 1 , 1 , - 4 } , { 1 , 1 , 1 , 1 } } ; System . out . println ( maxPlus ( arr ) ) ; } }