Find maximum sum from top to bottom row with no adjacent diagonal elements | C # implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver code ; Function to find maximum path
using System ; using System . Collections . Generic ; class GFG { static int maxSum ( int [ , ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { List < Tuple < int , int > > aux = new List < Tuple < int , int > > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . Add ( new Tuple < int , int > ( V [ i + 1 , j ] , j ) ) ; } aux . Sort ( ) ; aux . Reverse ( ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux [ k ] . Item2 - j == 0 || Math . Abs ( aux [ k ] . Item2 - j ) > 1 ) { V [ i , j ] += aux [ k ] . Item1 ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . Max ( ans , V [ 0 , i ] ) ; } return ans ; } static void Main ( ) { int [ , ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . GetLength ( 0 ) ; int m = V . GetLength ( 1 ) ; Console . WriteLine ( maxSum ( V , n , m ) ) ; } }