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
def solve ( n ) : NEW_LINE INDENT initial_array = [ [ 0 for i in range ( n - 1 ) ] for j in range ( n - 1 ) ] NEW_LINE final_array = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT initial_array [ 0 ] [ i ] = i + 1 NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] NEW_LINE DEDENT DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT final_array [ i ] [ j ] = initial_array [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT t0 = final_array [ i ] [ i ] NEW_LINE t1 = final_array [ i ] [ n - 1 ] NEW_LINE temp = final_array [ i ] [ i ] NEW_LINE final_array [ i ] [ i ] = final_array [ i ] [ n - 1 ] NEW_LINE final_array [ i ] [ n - 1 ] = temp NEW_LINE final_array [ n - 1 ] [ i ] = t0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( final_array [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE solve ( n ) NEW_LINE DEDENT