📌  相关文章
📜  检查对角线连接的按位与运算是否超过二进制矩阵的中间行/列元素的按位与运算

📅  最后修改于: 2021-05-25 01:51:03             🧑  作者: Mango

给定尺寸为N * N的二进制矩阵mat [] [] ,任务是检查通过连接主对角线和辅助对角线元素获得的十进制数的按位与,是否大于存在的元素获得的十进制数的按位与。在中间的行和列中。如果发现是真的,则打印“是” 。否则,打印“否”

注意:仅从左到右以及从上到下连接矩阵元素。如果N是偶数,则从两个中间的第一个中间行/列中取出。

例子:

天真的方法:解决问题的最简单方法是遍历给定的矩阵,并将对应的数字附加到变量(例如如果当前行等于当前列),则附加到变量(例如, S)(如果行是当前行) N列,一个变量,表示MR,如果行是等于N / 2,和给一个变量,表示MC,如果列是N / 2。完成上述步骤后,如果PS的按位AND大于MRMC的按位AND,则打印“是” 。否则,打印“否”。

下面是上述方法的实现:

Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
 
// Java Program for above approach
class GFG{
     
// Function to convert obtained binary
// representation to decimal value
static int convert(ArrayList p)
{
     
    // Stores the resultant number
    int ans = 0;
     
    // Traverse string arr
    for(int i: p)
    {
        ans = (ans << 1) | i;
    }
     
    // Return the number formed
    return ans;
}
 
// Function to count the number of
// set bits in the number num
static int count(int num)
{
     
    // Stores the count of set bits
    int ans = 0;
     
    // Iterate until num > 0
    while (num > 0)
    {
        ans += num & 1;
        num >>= 1;
    }
    return ans;
}
 
// Function to check if the given matrix
// satisfies the given condition or not
static void checkGoodMatrix(int mat[][])
{
    ArrayList P = new ArrayList();
    ArrayList S = new ArrayList();
    ArrayList MR = new ArrayList();
    ArrayList MC = new ArrayList();
     
    // To get P, S, MR, and MC
    for(int i = 0; i < mat.length; i++)
    {
        for(int j = 0; j < mat[0].length; j++)
        {
            if (i == j)
                P.add(mat[i][j]);
 
            if (i + j == mat.length - 1)
                S.add(mat[i][j]);
 
            if (i == Math.floor((mat.length - 1) / 2))
                MR.add(mat[i][j]);
 
            if (j == Math.floor((mat.length - 1) / 2))
                MC.add(mat[i][j]);
        }
    }
    Collections.reverse(S);
 
    // Stores decimal equivalents
    // of binary representations
    int P0 = convert(P);
    int S0 = convert(S);
    int MR0 = convert(MR);
    int MC0 = convert(MC);
 
    // Gett the number of set bits
    int setBitsPS = count((P0 & S0));
    int setBitsMM = count((MR0 & MC0));
     
    // Print the answer
    if (setBitsPS > setBitsMM)
       System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int mat[][] = { { 1, 0, 1 },
                    { 0, 0, 1 },
                    { 0, 1, 1 } };
    checkGoodMatrix(mat);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Functio to convert obtained binary
# representation to decimal value
def convert(arr):
   
      # Stores the resultant number
    ans = 0
     
    # Traverse string arr
    for i in arr:
        ans = (ans << 1) | i
         
    # Return the number formed
    return ans
 
# Function to count the number of
# set bits in the number num
def count(num):
   
    # Stores the count of set bits
    ans = 0
     
    # Iterate until num > 0
    while num:
        ans += num & 1
        num >>= 1
    return ans
 
# Function to check if the given matrix
# satisfies the given condition or not
def checkGoodMatrix(mat):
    P = []
    S = []
    MR = []
    MC = []
 
    # To get P, S, MR, and MC
    for i in range(len(mat)):
        for j in range(len(mat[0])):
 
            if i == j:
                P.append(mat[i][j])
 
            if i + j == len(mat)-1:
                S.append(mat[i][j])
 
            if i == (len(mat)-1)//2:
                MR.append(mat[i][j])
 
            if j == (len(mat)-1)//2:
                MC.append(mat[i][j])
 
    S.reverse()
 
    # Stores decimal equivalents
    # of binary representations
    P = convert(P)
    S = convert(S)
    MR = convert(MR)
    MC = convert(MC)
 
    # Gett the number of set bits
    setBitsPS = count(P & S)
    setBitsMM = count(MR & MC)
 
    # Print the answer
    if setBitsPS > setBitsMM:
        print("Yes")
    else:
        print("No")
 
# Driver Code
 
# Given Matrix
mat = [[1, 0, 1], [0, 0, 1], [0, 1, 1]]
 
checkGoodMatrix(mat)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to convert obtained binary
// representation to decimal value
static int convert(List p)
{
     
    // Stores the resultant number
    int ans = 0;
 
    // Traverse string arr
    foreach(int i in p)
    {
        ans = (ans << 1) | i;
    }
 
    // Return the number formed
    return ans;
}
 
// Function to count the number of
// set bits in the number num
static int count(int num)
{
     
    // Stores the count of set bits
    int ans = 0;
 
    // Iterate until num > 0
    while (num > 0)
    {
        ans += num & 1;
        num >>= 1;
    }
    return ans;
}
 
// Function to check if the given matrix
// satisfies the given condition or not
static void checkGoodMatrix(int[, ] mat)
{
    List P = new List();
    List S = new List();
    List MR = new List();
    List MC = new List();
 
    // To get P, S, MR, and MC
    for(int i = 0; i < mat.GetLength(0); i++)
    {
        for(int j = 0; j < mat.GetLength(1); j++)
        {
            if (i == j)
                P.Add(mat[i, j]);
 
            if (i + j == mat.GetLength(0) - 1)
                S.Add(mat[i, j]);
 
            if (i == Math.Floor(
                (mat.GetLength(0) - 1) / 2.0))
                MR.Add(mat[i, j]);
 
            if (j == Math.Floor(
                (mat.GetLength(0) - 1) / 2.0))
                MC.Add(mat[i, j]);
        }
    }
    S.Reverse();
 
    // Stores decimal equivalents
    // of binary representations
    int P0 = convert(P);
    int S0 = convert(S);
    int MR0 = convert(MR);
    int MC0 = convert(MC);
 
    // Gett the number of set bits
    int setBitsPS = count((P0 & S0));
    int setBitsMM = count((MR0 & MC0));
 
    // Print the answer
    if (setBitsPS > setBitsMM)
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main(string[] args)
{
    int[,] mat = { { 1, 0, 1 },
                   { 0, 0, 1 },
                   { 0, 1, 1 } };
                    
    checkGoodMatrix(mat);
}
}
 
// This code is contributed by ukasp


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the matrix
// satisfy the given condition or not
void checkGoodMatrix(
    vector > M, int N)
{
    // Stores the binary representation
    vector p, s, MR, MC;
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++) {
 
        // Push element of main diagonal
        p.push_back(M[i][i]);
 
        // Push element of cross diagona
        s.push_back(M[N - 1 - i][i]);
 
        // Push element of Mid row
        MR.push_back(M[(N - 1) / 2][i]);
 
        // Push element of Mid column
        MC.push_back(M[i][(N - 1) / 2]);
    }
 
    // Check if S & P > MR & MC
    for (int i = 0; i < N; i++) {
 
        if (p[i] & s[i] > MR[i] & MC[i]) {
            cout << "Yes";
            return;
        }
        else if (p[i] & s[i] < MR[i] & MC[i]) {
            cout << "No";
            return;
        }
    }
 
    cout << "No";
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > M{ { 0, 1, 1 },
                            { 0, 0, 0 },
                            { 0, 1, 1 } };
 
    // Size of the matrix
    int N = M.size();
 
    checkGoodMatrix(M, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Vector;
 
class GFG{
 
static void checkGoodMatrix(int[][] M, int N)
{
     
    // Stores the binary representation
    Vector p = new Vector();
    Vector s = new Vector();
    Vector MR = new Vector();
    Vector MC = new Vector();
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; i++)
    {
         
        // Push element of main diagonal
        p.add(M[i][i]);
 
        // Push element of cross diagona
        s.add(M[N - 1 - i][i]);
 
        // Push element of Mid row
        MR.add(M[(N - 1) / 2][i]);
 
        // Push element of Mid column
        MC.add(M[i][(N - 1) / 2]);
    }
 
    // Check if S & P > MR & MC
    for(int i = 0; i < N; i++)
    {
        int P = p.get(i);
        int S = s.get(i);
        int Mr = MR.get(i);
        int Mc = MC.get(i);
 
        if ((P & S) > (Mr & Mc))
        {
            System.out.print("Yes");
            return;
        }
        else if ((P & S) < (Mr & Mc))
        {
            System.out.print("No");
            return;
        }
    }
    System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given matrix
    int[][] M = { { 0, 1, 1 },
                  { 0, 0, 0 },
                  { 0, 1, 1 } };
 
    // Size of the matrix
    int N = M.length;
 
    checkGoodMatrix(M, N);
}
}
 
// This code is contributed by abhinavjain194


输出:
No

时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:要优化上述方法,可以通过仅遍历每个元素的对角线,中间行和中间列来优化上述方法。请按照以下步骤解决问题:

  • 初始化辅助向量,例如P,S,MRMC,以分别存储主对角线,交叉对角线,中间行和中间列的连接元素。
  • [0,N – 1]范围内迭代:
    • (i,i)处的元素附加到P (即主对角线)。
    • (N – 1 – i,i)处将元素附加到S ,即交叉对角线。
    • ((N-1)/ 2,i)处将元素追加到MR ,即中间行。
    • ((N-1)/ 2,i)处的元素追加到MC ,即中间列。
  • [0,N – 1]范围内迭代:
    • 检查P [i]和S [i]> MR [i]和MC [i],然后打印“是”并返回。
    • 否则,检查p [i]&s [i] ,然后打印“否”并返回。
  • 如果以上条件都不满足,则打印“否”

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the matrix
// satisfy the given condition or not
void checkGoodMatrix(
    vector > M, int N)
{
    // Stores the binary representation
    vector p, s, MR, MC;
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++) {
 
        // Push element of main diagonal
        p.push_back(M[i][i]);
 
        // Push element of cross diagona
        s.push_back(M[N - 1 - i][i]);
 
        // Push element of Mid row
        MR.push_back(M[(N - 1) / 2][i]);
 
        // Push element of Mid column
        MC.push_back(M[i][(N - 1) / 2]);
    }
 
    // Check if S & P > MR & MC
    for (int i = 0; i < N; i++) {
 
        if (p[i] & s[i] > MR[i] & MC[i]) {
            cout << "Yes";
            return;
        }
        else if (p[i] & s[i] < MR[i] & MC[i]) {
            cout << "No";
            return;
        }
    }
 
    cout << "No";
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > M{ { 0, 1, 1 },
                            { 0, 0, 0 },
                            { 0, 1, 1 } };
 
    // Size of the matrix
    int N = M.size();
 
    checkGoodMatrix(M, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.Vector;
 
class GFG{
 
static void checkGoodMatrix(int[][] M, int N)
{
     
    // Stores the binary representation
    Vector p = new Vector();
    Vector s = new Vector();
    Vector MR = new Vector();
    Vector MC = new Vector();
 
    // Iterate over the range [0, N]
    for(int i = 0; i < N; i++)
    {
         
        // Push element of main diagonal
        p.add(M[i][i]);
 
        // Push element of cross diagona
        s.add(M[N - 1 - i][i]);
 
        // Push element of Mid row
        MR.add(M[(N - 1) / 2][i]);
 
        // Push element of Mid column
        MC.add(M[i][(N - 1) / 2]);
    }
 
    // Check if S & P > MR & MC
    for(int i = 0; i < N; i++)
    {
        int P = p.get(i);
        int S = s.get(i);
        int Mr = MR.get(i);
        int Mc = MC.get(i);
 
        if ((P & S) > (Mr & Mc))
        {
            System.out.print("Yes");
            return;
        }
        else if ((P & S) < (Mr & Mc))
        {
            System.out.print("No");
            return;
        }
    }
    System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given matrix
    int[][] M = { { 0, 1, 1 },
                  { 0, 0, 0 },
                  { 0, 1, 1 } };
 
    // Size of the matrix
    int N = M.length;
 
    checkGoodMatrix(M, N);
}
}
 
// This code is contributed by abhinavjain194
输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)