Monday, October 7, 2024
Google search engine
HomeData Modelling & AIConstruct a square Matrix whose parity of diagonal sum is same as...

Construct a square Matrix whose parity of diagonal sum is same as size of matrix

Given an integer N representing the size of the matrix, the task is to construct a square matrix N * N which have an element from 1 to N2 such that the parity of the sum of its diagonals is equal to the parity of integer N.

Examples:

Input: N = 4
Output:
1   2   3   4 
8   7   6   5
9  10  11 12
16 15 14 13
Explanation:
Sum of diagonal = 32 and 36 and integer N = 4, all the numbers are even that is same parity.

Input: N = 3
Output:
1 2 3 
6 5 4
7 8 9
Explanation:
Sum of diagonal = 15 and integer N = 3, all the numbers are odd that is same parity.

Approach: The idea is to observe that on filling the elements in the matrix in an alternative fashion the parity of N and the sum of diagonals is the same. Start the counter from 1 and then fill the first row from 0 to N – 1 in increasing order, then fill the second row from index N – 1 to 0, and so on. Keep filling each element from value 1 to N2 in this alternate fashion to get the required matrix.
Below is the implementation of the above approach:
 

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to construct a N * N
// matrix based on the given condition
void createMatrix(int N)
{
    // Matrix with given sizM
    int M[N][N];
 
    // Counter to insert elements
    // from 1 to N * N
    int count = 1;
    for (int i = 0; i < N; i++) {
 
        // Check if it is first row
        // or odd row of the matrix
        if (i % 2 == 0) {
 
            for (int j = 0; j < N; j++) {
                M[i][j] = count;
                count++;
            }
        }
        else
        {
            // Insert elements from
            // right to left
            for (int j = N - 1;j >= 0; j--){
                M[i][j] = count;
                count += 1;
            }
        }
    }
 
    // Print the matrix
    for (int i = 0; i < N; i++) {
 
        // Traverse column
        for (int j = 0; j < N; j++) {
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given size of matrix N
    int N = 3;
 
    // Function Call
    createMatrix(N);
    return 0;
}


Java




// Java program for the above approach
class GFG{
  
// Function to construct a N * N
// matrix based on the given condition
static void createMatrix(int N)
{
    // Matrix with given sizM
    int M[][] = new int[N][N];
  
    // Counter to insert elements
    // from 1 to N * N
    int count = 1;
    for (int i = 0; i < N; i++)
    {
  
        // Check if it is first row
        // or odd row of the matrix
        if (i % 2 == 0)
        {
  
            for (int j = 0; j < N; j++)
            {
                M[i][j] = count;
                count++;
            }
        }
        else
        {
            // Insert elements from
            // right to left
            for(int j = N - 1; j >= 0; j--){
                M[i][j] = count;
                count += 1 ;
            }
        }
    }
  
    // Print the matrix
    for (int i = 0; i < N; i++)
    {
  
        // Traverse column
        for (int j = 0; j < N; j++)
        {
            System.out.print(M[i][j] + " ");
        }
        System.out.println();
    }
}
  
// Driver Code
public static void main(String[] args)
{
    // Given size of matrix N
    int N = 3;
  
    // Function Call
    createMatrix(N);
}
}
 
// This code is contributed by Ritik Bansal


Python3




# Python3 program for the above approach
 
# Function to construct a N * N
# matrix based on the given condition
def createMatrix(N):
     
    # Matrix with given size
    M = [[0] * N for i in range(N)]
 
    # Counter to insert elements
    # from 1 to N * N
    count = 1
     
    for i in range(N):
 
        # Check if it is first row
        # or odd row of the matrix
        if (i % 2 == 0):
            for j in range(N):
 
                # Insert elements from
                # left to right
                M[i][j] = count
                count += 1
 
        # Condition if it is second
        # row or even row
        else:
 
            # Insert elements from
            # right to left
            for j in range(N - 1, -1, -1):
                M[i][j] = count
                count += 1
 
    # Print the matrix
    for i in range(N):
 
        # Traverse column
        for j in range(N):
            print(M[i][j], end = " ")
 
        print()
 
# Driver Code
if __name__ == '__main__':
     
    # Given size of matrix N
    N = 3
 
    # Function call
    createMatrix(N)
 
# This code is contributed by mohit kumar 29


C#




// C# program for
// the above approach
using System;
class GFG{
  
// Function to construct a N * N
// matrix based on the given condition
static void createMatrix(int N)
{
  // Matrix with given sizM
  int[,] M = new int[N, N];
 
  // Counter to insert elements
  // from 1 to N * N
  int count = 1;
  for (int i = 0; i < N; i++)
  {
    // Check if it is first row
    // or odd row of the matrix
    if (i % 2 == 0)
    {
 
      for (int j = 0; j < N; j++)
      {
        M[i, j] = count;
        count++;
      }
    }
    else
    {
      // Insert elements from
      // right to left
      for(int j = N - 1; j >= 0; j--)
      {
        M[i, j] = count;
        count += 1;
      }
    }
  }
 
  // Print the matrix
  for (int i = 0; i < N; i++)
  {
    // Traverse column
    for (int j = 0; j < N; j++)
    {
      Console.Write(M[i, j] + " ");
    }
    Console.WriteLine();
  }
}
  
// Driver Code
public static void Main()
{
  // Given size of matrix N
  int N = 3;
 
  // Function Call
  createMatrix(N);
}
}
 
// This code is contributed by Chitranayal


Javascript




<script>
 
// javascript program for the above approach
 
  
// Function to construct a N * N
// matrix based on the given condition
function createMatrix(N)
{
    // Matrix with given sizM
    var M = Array(N).fill(0).map(x => Array(N).fill(0));
  
    // Counter to insert elements
    // from 1 to N * N
    var count = 1;
    for (i = 0; i < N; i++)
    {
  
        // Check if it is first row
        // or odd row of the matrix
        if (i % 2 == 0)
        {
  
            for (j = 0; j < N; j++)
            {
                M[i][j] = count;
                count++;
            }
        }
        else
        {
            // Insert elements from
            // right to left
            for(j = N - 1; j >= 0; j--){
                M[i][j] = count;
                count += 1 ;
            }
        }
    }
  
    // Print the matrix
    for (i = 0; i < N; i++)
    {
  
        // Traverse column
        for (j = 0; j < N; j++)
        {
            document.write(M[i][j] + " ");
        }
        document.write("<br>");
    }
}
  
// Driver Code
var N = 3;
 
// Function Call
createMatrix(N);
 
// This code is contributed by 29AjayKumar
 
</script>


Output: 

1 2 3 
6 5 4 
7 8 9

 

Time Complexity: O(N*N) 
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments