Python3 Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square.
Examples:
Input: N = 3
Output:
1 2 3
2 3 1
3 2 1
Explanation:
The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32).Input: N = 7
Output:
1 2 3 4 5 6 7
2 3 4 5 6 7 1
3 4 5 6 7 1 2
4 5 6 7 1 2 3
5 6 7 1 2 3 4
6 7 1 2 3 4 5
7 1 2 3 4 5 6
Explanation:
The sum of secondary diagonal = 7 + 7 + 7 + 7 + 7 + 7 + 7 = 49(= 72).
Approach: Since the generated matrix needs to be of dimensions N x N, therefore, to make the sum of elements in the secondary diagonal a perfect square, the idea is to assign N at each index of the secondary diagonal. Therefore, the sum of all N elements in this diagonal is N2, which is a perfect square. Follow the steps below to solve the problem:
- Initialize a matrix mat[][] of dimension N x N.
- Initialize the first row of the matrix as {1 2 3 … N}.
- Now for the remaining rows of the matrix, fill each row by circular left shift of the arrangement of the previous row of the matrix by 1.
- Print the matrix after completing the above steps.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach # Function to print the matrix whose sum # of element in secondary diagonal is a # perfect square def diagonalSumPerfectSquare(arr, N): # Print the current row print ( * arr, sep = " " ) # Iterate for next N - 1 rows for i in range (N - 1 ): # Perform left shift by 1 arr = arr[i::] + arr[:i:] # Print the current row after # the left shift print ( * arr, sep = " " ) # Driver Code # Given N N = 7 arr = [] # Fill the array with elements # ranging from 1 to N for i in range ( 1 , N + 1 ): arr.append(i) # Function Call diagonalSumPerfectSquare(arr, N) |
1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6
Time Complexity: O(N2)
Auxiliary Space: O(N)
Method 2: Using nested for loops
Step-by-step approach:
- Initialize an empty list arr and a variable N representing the size of the matrix.
- Fill the list arr with elements ranging from 1 to N.
- Create a 2D matrix of size N x N with all elements initialized to 0.
- Fill the matrix with the elements of arr such that the element in row i and column j is arr[(i+j) % N].
- Print the matrix row-wise.
- Compute the sum of elements in the secondary diagonal by adding the elements at positions (0,N-1), (1,N-2), …, (N-1,0).
- Check if the sum is a perfect square. If it is, print the matrix. If not, do not print the matrix.
Below is the implementation of the above approach:
Python3
# Function to print the matrix whose sum # of element in secondary diagonal is a # perfect square def diagonalSumPerfectSquare(arr, N): # Initialize the matrix matrix = [[ 0 for x in range (N)] for y in range (N)] # Fill the matrix with the elements of arr for i in range (N): for j in range (N): matrix[i][j] = arr[(i + j) % N] # Print the current row for i in range (N): print ( * matrix[i], sep = " " ) # Driver Code # Given N N = 7 arr = [] # Fill the array with elements # ranging from 1 to N for i in range ( 1 , N + 1 ): arr.append(i) # Function Call diagonalSumPerfectSquare(arr, N) |
1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6
Time complexity: O(N^2) as we iterate over the matrix using nested for loops.
Auxiliary space: O(N^2) as we create a 2D matrix to store the elements.
Please refer complete article on Generate a matrix having sum of secondary diagonal equal to a perfect square for more details!