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
function maxSum ( V , n , m ) { let ans = 0 ; for ( let i = n - 2 ; i >= 0 ; -- i ) { let aux = new Array ( ) ; for ( let j = 0 ; j < m ; ++ j ) { aux . push ( [ V [ i + 1 ] [ j ] , j ] ) ; } aux . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ; aux . reverse ( ) ; for ( let j = 0 ; j < m ; ++ j ) { for ( let k = 0 ; k < m ; ++ k ) { if ( aux [ k ] [ 1 ] - j == 0 || Math . abs ( aux [ k ] [ 1 ] - j ) > 1 ) { V [ i ] [ j ] += aux [ k ] [ 0 ] ; break ; } } } } for ( let i = 0 ; i < m ; ++ i ) { ans = Math . max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } let V = [ [ 1 , 2 , 3 , 4 ] , [ 8 , 7 , 6 , 5 ] , [ 10 , 11 , 12 , 13 ] ] ; let n = V . length ; let m = V [ 0 ] . length ; document . write ( maxSum ( V , n , m ) ) ;