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

📅  最后修改于: 2021-04-27 20:28:30             🧑  作者: 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


输出:
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)