给定的数N,所述任务是构建的N * N的方矩阵其中在一些元素的联合i相第i列第i行包含在范围[1,2 * N-1]的每一个元素。如果不存在这样的矩阵,则打印 -1。
注意:对于特定的 N 可以有多种可能的解决方案
例子:
Input: N = 6
Output:
6 4 2 5 3 1
10 6 5 3 1 2
8 11 6 1 4 3
11 9 7 6 2 4
9 7 10 8 6 5
7 8 9 10 11 6
Explanation:
The above matrix is of 6 * 6 in which every ith row and column contains elements from 1 to 11, like:
1st row and 1st column = {6, 4, 2, 5, 3, 1} & {6, 10, 8, 11, 9, 7} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
2nd row and 2nd column = {10, 6, 5, 3, 1, 2} & {4, 6, 11, 9, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
3rd row and 3rd column = {8, 11, 6, 1, 4, 3} & {2, 5, 6, 7, 10, 9} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
4th row and 4th column = {11, 9, 7, 6, 2, 4} & {5, 3, 1, 6, 8, 10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
5th row and 5th column = {9, 7, 10, 8, 6, 5} & {3, 1, 4, 2, 6, 11} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
6th row and 6th column = {7, 8, 9, 10, 11, 6} & {1, 2, 3, 4, 5, 6} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Input: N = 6
Output: -1
Explanation:
There is no such matrix possible in which every i’th row and i’th column contains every elements from 1 to 9. Hence, the answer is -1.
方法:
如果我们仔细观察,我们可以看到:
- 对于除 1 以外的任何奇数,方阵是不可能生成的
- 生成偶数阶的方阵,思路是将矩阵的对角线元素的上半部分在1到N-1的范围内填满,所有的对角线元素都用N和对角线的下半部分进行填充元素可以填写从范围N + 1 到 2N – 1 的数字。
下面是这种方法的算法:
- 除了N = 1外,无法填充奇数阶矩阵
- 对于偶数阶矩阵,
- 首先,填充所有等于 N 的对角线元素。
- 考虑对角二等分矩阵的两半,每一半可以用 N-1 个元素填充。
- 用[1, N-1] 中的元素填充上半部分,用[N+1, 2N-1] 中的元素填充下半部分。
- 可以很容易地观察到,存在第二行的最后一个元素始终为 2 的模式。
- 现在,最后一列中的连续元素相差 2。因此,对于所有来自 1 的 i,广义形式可以表示为A[i]=[(N-2)+2i]%(N-1)+1到 N-1
- 只需将N添加到下半部分的所有元素。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
int matrix[100][100];
// Function to find the square matrix
void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
cout << "1"
<< "\n";
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
cout << "-1"
<< "\n";
}
// For Matrix of even order
else {
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i][i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
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;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i][j] = matrix[j][i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << "\n";
}
}
cout << "\n";
}
// Driver Code
int main()
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
return 0;
}
Java
// Java implementation of the above approach
class GFG {
static int matrix[][] = new int[100][100];
// Function to find the square matrix
static void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
System.out.println("1");
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
System.out.println("-1");
}
// For Matrix of even order
else {
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i][i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
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) {
int temp = a;
a = b;
b = temp;
}
matrix[b][a] = i + 1;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i][j] = matrix[j][i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(matrix[i][j] + " ");
System.out.println() ;
}
}
System.out.println();
}
// Driver Code
public static void main (String[] args)
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach
import numpy as np;
matrix = np.zeros((100,100));
# Function to find the square matrix
def printRequiredMatrix(n) :
# For Matrix of order 1,
# it will contain only 1
if (n == 1) :
print("1");
# For Matrix of odd order,
# it is not possible
elif (n % 2 != 0) :
print("-1");
# For Matrix of even order
else :
# All diagonal elements of the
# matrix can be N itself.
for i in range(n) :
matrix[i][i] = n;
u = n - 1;
# Assign values at desired
# place in the matrix
for i in range(n - 1) :
matrix[i][u] = i + 1;
for j in range(1, n//2) :
a = (i + j) % (n - 1);
b = (i - j + n - 1) % (n - 1);
if (a < b) :
a,b = b,a
matrix[b][a] = i + 1;
# Loop to add N in the lower half
# of the matrix such that it contains
# elements from 1 to 2*N - 1
for i in range(n) :
for j in range(i) :
matrix[i][j] = matrix[j][i] + n;
# Loop to print the matrix
for i in range(n) :
for j in range(n) :
print(matrix[i][j] ,end=" ");
print();
print()
# Driver Code
if __name__ == "__main__" :
n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach
using System;
class GFG {
static int [,]matrix = new int[100, 100];
// Function to find the square matrix
static void printRequiredMatrix(int n)
{
// For Matrix of order 1,
// it will contain only 1
if (n == 1) {
Console.WriteLine("1");
}
// For Matrix of odd order,
// it is not possible
else if (n % 2 != 0) {
Console.WriteLine("-1");
}
// For Matrix of even order
else
{
// All diagonal elements of the
// matrix can be N itself.
for (int i = 0; i < n; i++) {
matrix[i, i] = n;
}
int u = n - 1;
// Assign values at desired
// place in the matrix
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) {
int temp = a;
a = b;
b = temp;
}
matrix[b, a] = i + 1;
}
}
// Loop to add N in the lower half
// of the matrix such that it contains
// elements from 1 to 2*N - 1
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
matrix[i, j] = matrix[j, i] + n;
// Loop to print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
Console.Write(matrix[i, j] + " ");
Console.WriteLine() ;
}
}
Console.WriteLine();
}
// Driver Code
public static void Main (String[] args)
{
int n = 1;
printRequiredMatrix(n);
n = 3;
printRequiredMatrix(n);
n = 6;
printRequiredMatrix(n);
}
}
// This code is contributed by Yash_R
Javascript
1
-1
6 4 2 5 3 1
10 6 5 3 1 2
8 11 6 1 4 3
11 9 7 6 2 4
9 7 10 8 6 5
7 8 9 10 11 6
性能分析:
- 时间复杂度:在上面的方法中,有两个循环来迭代整个 N*N 矩阵,在最坏的情况下需要 O(N 2 ) 时间,因此时间复杂度将为O(N 2 ) 。
- 辅助空间复杂度:与上述方法一样,没有使用额外的空间,因此辅助空间复杂度为O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live