Generate a matrix having sum of secondary diagonal equal to a perfect square
Last Updated :
23 Nov, 2021
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:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
void diagonalSumPerfectSquare(int arr[], int N)
{
// Iterate for next N - 1 rows
for(int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for(int j = 0; j < N; j++)
{
cout << (arr[(j + i) % 7]) << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
// Given N
int N = 7;
int arr[N];
// Fill the array with elements
// ranging from 1 to N
for(int i = 0; i < N; i++)
{
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
// This code is contributed by gauravrajput1
Java
// Java program for the above approach
class GFG {
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
static void diagonalSumPerfectSquare(int[] arr,
int N)
{
// Iterate for next N - 1 rows
for (int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for (int j = 0; j < N; j++)
{
System.out.print(arr[(j + i) % 7] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] srgs)
{
// Given N
int N = 7;
int[] arr = new int[N];
// Fill the array with elements
// ranging from 1 to N
for (int i = 0; i < N; i++)
{
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
}
// This code is contributed by Amit Katiyar
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)
C#
// C# program for the
// above approach
using System;
class GFG {
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
static void diagonalSumPerfectSquare(int[] arr,
int N)
{
// Iterate for next N - 1 rows
for (int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for (int j = 0; j < N; j++)
{
Console.Write(arr[(j + i) % 7] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main(String[] srgs)
{
// Given N
int N = 7;
int[] arr = new int[N];
// Fill the array with elements
// ranging from 1 to N
for (int i = 0; i < N; i++) {
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
function diagonalSumPerfectSquare( arr,N)
{
// Iterate for next N - 1 rows
for (let i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for (let j = 0; j < N; j++)
{
document.write(arr[(j + i) % 7] + " ");
}
document.write("<br/>");
}
}
// Driver Code
// Given N
let N = 7;
let arr = new Array(N).fill(0);
// Fill the array with elements
// ranging from 1 to N
for (let i = 0; i < N; i++)
{
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
// This code is contributed by avijitmondal1998.
</script>
Output1 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)
Similar Reads
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices Given a positive integer N, the task is to construct a matrix of size N * N such that all the matrix elements are distinct from the range [1, N2] and the sum of elements in both the diagonals of every 2 * 2 submatrices is even. Examples: Input: N = 3 Output: 1 2 3 4 5 6 7 8 9 Explanation: Diagonal e
7 min read
Generate a matrix having each element equal to the sum of specified submatrices of a given matrix Given a matrix mat[][] of dimensions M*N and an integer K, the task is to generate a matrix answer[][], where answer[i][j] is equal to the sum of all elements mat[r][c][/c] such that r â [i - K, i + K], c â [j - K, j + K], and (r, c) is a valid position in the matrix. Examples: Input: mat = {{1, 2,
15+ min read
Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square Given a positive integer N, the task is to find the number of unique permutations of first N natural numbers having sum of the adjacent elements equal to a perfect square. Examples: Input: N = 17Output: 2Explanation:Following permutations have sum of adjacent elements equal to a perfect square: {17,
8 min read
Largest square sub-matrix with equal row, column, and diagonal sum Given a matrix mat[][] of dimensions N*M, the task is to find the size of the largest square submatrix such that the sum of all rows, columns, diagonals in that submatrix are equal.Examples:Input: N = 3, M = 4, mat[][] = [[5, 1, 3, 1], [9, 3, 3, 1], [1, 3, 3, 8]]Output: 2Explanation:The submatrix wh
11 min read
Sum of all parts of a square Matrix divided by its diagonals Given a 2D matrix arr[][] of N*N dimensions, the task is to find the sum of elements of all four parts of the matrix divided by the diagonals without including the diagonal elements in any of the four parts.Example: Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } Output: 2 4 6 8 Explanation: (1
9 min read