Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Prthe matrix ; Driver Code
def generateMatrix ( N ) : NEW_LINE INDENT odd = 1 ; NEW_LINE even = 2 ; NEW_LINE mat = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( ( i + j ) % 2 == 0 ) : NEW_LINE INDENT mat [ i ] [ j ] = odd ; NEW_LINE odd += 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] [ j ] = even ; NEW_LINE even += 2 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE generateMatrix ( N ) ; NEW_LINE DEDENT