📌  相关文章
📜  构造一个矩阵,使得第 i 行和第 i 列的并集包含从 1 到 2N-1 的每个元素

📅  最后修改于: 2021-09-04 08:28:19             🧑  作者: Mango

给定的数N,所述任务是构建的N * N方矩阵其中在一些元素的联合i第i第i行包含在范围[1,2 * N-1]的每一个元素。如果不存在这样的矩阵,则打印 -1。
注意:对于特定的 N 可以有多种可能的解决方案
例子:

方法:
如果我们仔细观察,我们可以看到:

  • 对于除 1 以外的任何奇数,方阵是不可能生成的
  • 生成偶数阶的方阵,思路是将矩阵的对角线元素的上半部分在1到N-1的范围内填满,所有的对角线元素都用N对角线下半部分进行填充元素可以填写从范围N + 1 到 2N – 1 的数字

下面是这种方法的算法:

  1. 除了N = 1外,无法填充奇数阶矩阵
  2. 对于偶数阶矩阵,
    • 首先,填充所有等于 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