Find the maximum sum of Plus shape pattern in a 2 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE const int n = 3 , m = 4 ; int maxPlus ( int ( & arr ) [ n ] [ m ] ) { int ans = INT_MIN ; int left [ N ] [ N ] , right [ N ] [ N ] , up [ N ] [ N ] , down [ N ] [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { left [ i ] [ j ] = max ( 0LL , ( j ? left [ i ] [ j - 1 ] : 0LL ) ) + arr [ i ] [ j ] ; up [ i ] [ j ] = max ( 0LL , ( i ? up [ i - 1 ] [ j ] : 0LL ) ) + arr [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { right [ i ] [ j ] = max ( 0LL , ( j + 1 == m ? 0LL : right [ i ] [ j + 1 ] ) ) + arr [ i ] [ j ] ; down [ i ] [ j ] = max ( 0LL , ( i + 1 == n ? 0LL : down [ i + 1 ] [ j ] ) ) + arr [ i ] [ j ] ; } } for ( int i = 1 ; i < n - 1 ; ++ i ) for ( int j = 1 ; j < m - 1 ; ++ j ) ans = max ( ans , up [ i - 1 ] [ j ] + down [ i + 1 ] [ j ] + left [ i ] [ j - 1 ] + right [ i ] [ j + 1 ] + arr [ i ] [ j ] ) ; return ans ; } int main ( ) { int arr [ n ] [ m ] = { { 1 , 1 , 1 , 1 } , { -6 , 1 , 1 , -4 } , { 1 , 1 , 1 , 1 } } ; cout << maxPlus ( arr ) ; return 0 ; }