Find a Symmetric matrix of order N that contain integers from 0 to N | Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver Code
function solve ( n ) { let initial_array = new Array ( n - 1 ) ; for ( var i = 0 ; i < initial_array . length ; i ++ ) { initial_array [ i ] = new Array ( 2 ) ; } let final_array = new Array ( n ) ; for ( var i = 0 ; i < final_array . length ; i ++ ) { final_array [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ i ] = i + 1 ; for ( let i = 1 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] ; for ( let i = 0 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) final_array [ i ] [ j ] = initial_array [ i ] [ j ] ; for ( let i = 0 ; i < n ; ++ i ) final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let t0 = final_array [ i ] [ i ] ; let t1 = final_array [ i ] [ n - 1 ] ; let s = final_array [ i ] [ i ] ; final_array [ i ] [ i ] = final_array [ i ] [ n - 1 ] ; final_array [ i ] [ n - 1 ] = s ; final_array [ n - 1 ] [ i ] = t0 ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < n ; ++ j ) document . write ( final_array [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let n = 5 ; solve ( n ) ;