Find the sum of the diagonal elements of the given N X N spiral matrix | Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code
let findSum ( n ) { let dp = new Array ( n + 1 ) ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } let n = 4 ; document . write ( findSum ( n ) ) ;