Given an integer N, the task is to construct a matrix M[][] of size N x N with numbers in the range [1, N^2] with the following conditions :
- The elements of the matrix M should be an integer between 1 and N^2.
- All elements of the matrix M are pairwise distinct.
- For each square submatrix containing cells in row r through r+a and in columns c through c+a(inclusive) for some valid integers r,c and a>=0: M(r,c)+M(r+a,c+a) is even and M(r,c+a)+M(r+a,c) is even.
Â
Examples:Â
Â
Input: N = 2Â
Output:Â
1 2Â
4 3Â
Explanation:Â
This matrix has 5 square submatrix and 4 of them ([1], [2], [3], [4]) have a=0 so they satisfy the conditions.Â
The last square submatrix is the whole matrix M where r=c=a=1. We can see that M(1, 1)+M(2, 2)=1+3=4 and M(1, 2)+M(2, 1)=2+4=6 are both even.
Input: N = 4Â
Output:Â
1 2 3 4Â
8 7 6 5Â
9 10 11 12Â
16 15 14 13Â
Â
Â
Â
Approach: We know that the sum of two numbers is even when their parity is the same. Let us say the parity of M(i, j) is odd that means the parity of M(i+1, j+1), M(i+1, j-1), M(i-1, j+1), M(i-1, j-1) has to be odd.
Below is the illustration for N = 4 to generate a matrix of size 4×4:Â
Â
Â
Â
So from the above illustration we have to fill the matrix in the Checkerboard Pattern. We can fill it in two ways:Â
Â
Â
- All black cells have an odd integer and white cells have an even integer.
- All black cells have an even integer and white cells have an odd integer.
Â
Below is the implementation of the above approach:
 Â
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; Â
// Function to print the desired matrix void UniqueMatrix( int N) { Â Â Â Â int element_value = 1; Â Â Â Â int i = 0; Â
    // element_value will start from 1     // and go up to N ^ 2 Â
    // i is row number and it starts     // from 0 and go up to N-1          // Iterate over all [0, N]     while (i < N)     {                  // If is even         if (i % 2 == 0)         {             for ( int f = element_value;                     f < element_value + N; f++)             {                                  // If row number is even print                 // the row in forward order                 cout << f << " " ;             }             element_value += N;         }         else         {             for ( int k = element_value + N - 1;                     k > element_value - 1; k--)             {                                  // If row number is odd print                 // the row in reversed order                 cout << k << " " ; Â
            }             element_value += N;         }         cout << endl;         i = i + 1;     } } Â
// Driver Code int main() {          // Given matrix size     int N = 4; Â
    // Function call     UniqueMatrix(N); } Â
// This code is contributed by chitranayal |
Java
// Java program for the above approach public class Gfg {        // Function to print the desired matrix     public static void UniqueMatrix( int N)     {         int element_value = 1 ;         int i = 0 ;                  // element_value will start from 1         // and go up to N ^ 2            // i is row number and it starts         // from 0 and go up to N-1                // Iterate over all [0, N]         while (i < N)         {             // If is even             if (i % 2 == 0 )             {                 for ( int f = element_value;                     f < element_value + N; f++)                 {                                        // If row number is even print                     // the row in forward order                     System.out.print(f+ " " );                 }                 element_value += N;             }             else             {                 for ( int k = element_value + N - 1 ;                     k > element_value - 1 ; k--)                 {                                        // If row number is odd print                     // the row in reversed order                     System.out.print(k+ " " );                 }                 element_value += N;             }             System.out.println();             i = i + 1 ;         }     }        // Driver Code     public static void main(String []args)     {                // Given matrix size         int N = 4 ;                // Function call         UniqueMatrix(N);     } } Â
// This code is contributed by avanitrachhadiya2155 |
Python3
# Python3 program for the above approach Â
# Function to print the desired matrix def UniqueMatrix(N): Â
    element_value = 1     i = 0 Â
    # element_value will start from 1     # and go up to N ^ 2 Â
    # i is row number and it starts     # from 0 and go up to N-1          # Iterate ove all [0, N]     while (i < N):                  # If is even         if (i % 2 = = 0 ): Â
            for f in range (element_value, element_value + N, 1 ): Â
                # If row number is even print                 # the row in forward order                 print (f, end = ' ' )             element_value + = N Â
        else : Â
            for k in range (element_value + N - 1 , element_value - 1 , - 1 ): Â
                # if row number is odd print                 # the row in reversed order                 print (k, end = ' ' ) Â
            element_value + = N Â
        print ()         i = i + 1 Â
Â
# Driver Code Â
# Given Matrix Size N = 4 Â
# Function Call UniqueMatrix(N) |
C#
// C# program for the above approach using System; class GFG {     static void UniqueMatrix( int N)     {         int element_value = 1;         int i = 0;                   // element_value will start from 1         // and go up to N ^ 2             // i is row number and it starts         // from 0 and go up to N-1                 // Iterate ove all [0, N]         while (i < N)         {                        // If is even             if (i % 2 == 0)             {                 for ( int f = element_value;                     f < element_value + N; f++)                 {                                        // If row number is even print                     // the row in forward order                     Console.Write(f + " " );                 }                 element_value += N;             }             else             {                 for ( int k = element_value + N - 1;                     k > element_value - 1; k--)                 {                     Console.Write(k + " " );                 }                 element_value += N;             }             Console.WriteLine();             i = i + 1;         }     }      // Driver code   static public void Main ()   {     // Given matrix size     int N = 4; Â
    // Function call     UniqueMatrix(N);   } } Â
// This code is contributed by rag2127 |
Javascript
<script> // Java script program for the above approach Â
    // Function to print the desired matrix     function  UniqueMatrix( N)     {         let element_value = 1;         let i = 0;                  // element_value will start from 1         // and go up to N ^ 2 Â
        // i is row number and it starts         // from 0 and go up to N-1              // Iterate ove all [0, N]         while (i < N)         {             // If is even             if (i % 2 == 0)             {                 for (let f = element_value;                     f < element_value + N; f++)                 {                                      // If row number is even print                     // the row in forward order                     document.write(f+ " " );                 }                 element_value += N;             }             else             {                 for (let k = element_value + N - 1;                     k > element_value - 1; k--)                 {                                      // If row number is odd print                     // the row in reversed order                     document.write(k+ " " );                 }                 element_value += N;             }             document.write( "<br>" );             i = i + 1;         }     } Â
    // Driver Code              // Given matrix size         let N = 4;              // Function call         UniqueMatrix(N); Â
// contributed by sravan kumar </script> |
1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13
Â
Time Complexity: O(N^2)Â
Auxiliary Space: O(1)Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!