Construct a matrix such that union of ith row and ith column contains every element from 1 to 2 N | C ++ implementation of the above approach ; Function to find the square matrix ; For Matrix of order 1 , it will contain only 1 ; For Matrix of odd order , it is not possible ; For Matrix of even order ; All diagonal elements of the matrix can be N itself . ; Assign values at desired place in the matrix ; Loop to add N in the lower half of the matrix such that it contains elements from 1 to 2 * N - 1 ; Loop to print the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int matrix [ 100 ] [ 100 ] ; void printRequiredMatrix ( int n ) { if ( n == 1 ) { cout << "1" << " STRNEWLINE " ; } else if ( n % 2 != 0 ) { cout << " - 1" << " STRNEWLINE " ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i ] [ i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i ] [ u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) swap ( a , b ) ; matrix [ b ] [ a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << matrix [ i ] [ j ] << " ▁ " ; cout << " STRNEWLINE " ; } } cout << " STRNEWLINE " ; } int main ( ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; return 0 ; }