Maximum sum in a 2 x n grid such that no two elements are adjacent | C # program Code for Maximum sum in a 2 x n grid such that no two elements are adjacent ; Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's  element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't  to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver Code
using System ; class GFG { public static int maxSum ( int [ , ] grid , int n ) { int incl = Math . Max ( grid [ 0 , 0 ] , grid [ 1 , 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = Math . Max ( excl , incl ) ; incl = excl + Math . Max ( grid [ 0 , i ] , grid [ 1 , i ] ) ; excl = excl_new ; } return Math . Max ( excl , incl ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } } ; int n = 5 ; Console . Write ( maxSum ( grid , n ) ) ; } }