Check if Bitwise AND of concatenation of diagonals exceeds that of middle row/column elements of a Binary Matrix

Check if Bitwise AND of concatenation of diagonals exceeds that of middle row/column elements of a Binary MatrixGiven a binary matrix mat[][] of dimensions N * N, the task is to check if Bitwise AND of the decimal numbers obtained by concatenating primary and secondary diagonals elements is greater than the Bitwise AND of the decimal numbers obtained by the elements present in the middle row and column. If found to be true, then print “Yes”. Otherwise, print “No”.Note: Concatenate matrix elements from left to right and top to bottom only. If N is even, then take the first middle row/column out of the two.Examples:Input: M[][] = {{1, 0, 1}, {0, 0, 1}, {0, 1, 1}}Output: NoExplanation: Number formed by concatenating principal diagonal elements is “101”.Number formed by concatenating cross diagonal elements is “001”.Number formed by concatenating elements in the middle row is “001”.Number formed by concatenating elements in the middle column is “001”.Therefore, the Bitwise AND of “101” and “001” is same as Bitwise AND of “001” and “001”. Input: M[][] = {{0, 1, 1}, {0, 0, 0}, {0, 1, 1}}Output: YesNaive Approach: The simplest approach to solve the problem is to traverse the given matrix and append the corresponding number to a variable, say P, if the current row is equal to the current column, to a variable, say S, if the row is N-column, to a variable,  say MR, if the row is equal to N/2, and to a variable, say MC, if the column is N/2. After completing the above steps, if Bitwise AND of P and S is greater than Bitwise AND of MR and MC, print “Yes”. Otherwise, print “No”.Below is the implementation of the above approach:Python3  def convert(arr):              ans = 0              for i in arr:        ans = (ans >= 1    return ans  def checkGoodMatrix(mat):    P = []    S = []    MR = []    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()              P = convert(P)    S = convert(S)    MR = convert(MR)    MC = convert(MC)          setBitsPS = count(P & S)    setBitsMM = count(MR & MC)          if setBitsPS > setBitsMM:        print(“Yes”)    else:        print(“No”)    mat = [[1, 0, 1], [0, 0, 1], [0, 1, 1]]  checkGoodMatrix(mat)Output:
No
Time Complexity: O(N2)Auxiliary Space: O(N)Efficient approach: To optimize the above approach, the above approach can be optimized by traversing on each element’s diagonals, middle row, and middle column only. Follow the steps below to solve the problem:Initialize auxiliary vectors, say P, S, MR, and MC to store the connected elements of the main diagonal, cross diagonal, mid-row, and mid-column respectively.Iterate over the range [0, N – 1]:Append the element at (i, i) to P, i.e., main diagonal.Append the element at (N – 1 – i, i) to S, i.e., cross diagonal.Append the element at ((N-1)/2, i) to MR, i.e., mid-row.Append the element at ((N-1)/2, i) to MC, i.e., mid-column.Iterate over the range [0, N – 1]:Check if P[i] & S[i] > MR[i] & MC[i], then print “Yes” and return.Otherwise, check if p[i] & s[i] < MR[i] & MC[i], then print “No” and return.If none of the above conditions satisfy, then print “No”.Below is the implementation of the above approach:C++  #include using namespace std;  void checkGoodMatrix(    vector M, int N){        vector p, s, MR, MC;          for (int i = 0; i < N; i++) {                  p.push_back(M[i][i]);                  s.push_back(M[N - 1 - i][i]);                  MR.push_back(M[(N - 1) / 2][i]);                  MC.push_back(M[i][(N - 1) / 2]);    }          for (int i = 0; i < N; i++) {          if (p[i] & s[i] > MR[i] & MC[i]) {            cout