Check if Bitwise AND of concatenation of diagonals exceeds that of middle row/column elements of a Binary Matrix
Given 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: No
Explanation:
The number formed by concatenating principal diagonal elements is “101”.
The number formed by concatenating cross diagonal elements is “001”.
The number formed by concatenating elements in the middle row is “001”.
The number formed by concatenating elements in the middle column is “001”.
Therefore, the Bitwise AND of “101” and “001” is the same as the Bitwise AND of “001” and “001”.
Input: M[][] = {{0, 1, 1}, {0, 0, 0}, {0, 1, 1}}
Output: Yes
Naive 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:
C++
#include <bits/stdc++.h>
using namespace std;
int convert(vector< int > p)
{
int ans = 0;
for ( int i : p)
{
ans = (ans << 1) | i;
}
return ans;
}
int count( int num)
{
int ans = 0;
while (num > 0)
{
ans += num & 1;
num >>= 1;
}
return ans;
}
void checkGoodMatrix(vector<vector< int > > mat)
{
vector< int > P;
vector< int > S;
vector< int > MR;
vector< int > MC;
for ( int i = 0; i < mat.size(); i++)
{
for ( int j = 0; j < mat[0].size(); j++)
{
if (i == j)
P.push_back(mat[i][j]);
if (i + j == mat.size() - 1)
S.push_back(mat[i][j]);
if (i == floor ((mat.size() - 1) / 2))
MR.push_back(mat[i][j]);
if (j == floor ((mat.size() - 1) / 2))
MC.push_back(mat[i][j]);
}
}
reverse(S.begin(), S.end());
int P0 = convert(P);
int S0 = convert(S);
int MR0 = convert(MR);
int MC0 = convert(MC);
int setBitsPS = count((P0 & S0));
int setBitsMM = count((MR0 & MC0));
if (setBitsPS > setBitsMM)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
vector<vector< int >> mat = { { 1, 0, 1 },
{ 0, 0, 1 },
{ 0, 1, 1 } };
checkGoodMatrix(mat);
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
class GFG{
static int convert(ArrayList<Integer> p)
{
int ans = 0 ;
for ( int i: p)
{
ans = (ans << 1 ) | i;
}
return ans;
}
static int count( int num)
{
int ans = 0 ;
while (num > 0 )
{
ans += num & 1 ;
num >>= 1 ;
}
return ans;
}
static void checkGoodMatrix( int mat[][])
{
ArrayList<Integer> P = new ArrayList<Integer>();
ArrayList<Integer> S = new ArrayList<Integer>();
ArrayList<Integer> MR = new ArrayList<Integer>();
ArrayList<Integer> MC = new ArrayList<Integer>();
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);
int P0 = convert(P);
int S0 = convert(S);
int MR0 = convert(MR);
int MC0 = convert(MC);
int setBitsPS = count((P0 & S0));
int setBitsMM = count((MR0 & MC0));
if (setBitsPS > setBitsMM)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 0 , 1 },
{ 0 , 0 , 1 },
{ 0 , 1 , 1 } };
checkGoodMatrix(mat);
}
}
|
Python3
def convert(arr):
ans = 0
for i in arr:
ans = (ans << 1 ) | i
return ans
def count(num):
ans = 0
while num:
ans + = num & 1
num >> = 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)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int convert(List< int > p)
{
int ans = 0;
foreach ( int i in p)
{
ans = (ans << 1) | i;
}
return ans;
}
static int count( int num)
{
int ans = 0;
while (num > 0)
{
ans += num & 1;
num >>= 1;
}
return ans;
}
static void checkGoodMatrix( int [, ] mat)
{
List< int > P = new List< int >();
List< int > S = new List< int >();
List< int > MR = new List< int >();
List< int > MC = new List< int >();
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();
int P0 = convert(P);
int S0 = convert(S);
int MR0 = convert(MR);
int MC0 = convert(MC);
int setBitsPS = count((P0 & S0));
int setBitsMM = count((MR0 & MC0));
if (setBitsPS > setBitsMM)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main( string [] args)
{
int [,] mat = { { 1, 0, 1 },
{ 0, 0, 1 },
{ 0, 1, 1 } };
checkGoodMatrix(mat);
}
}
|
Javascript
<script>
function convert(p) {
let ans = 0;
for (let i of p) {
ans = (ans << 1) | i;
}
return ans;
}
function count(num) {
let ans = 0;
while (num > 0) {
ans += num & 1;
num >>= 1;
}
return ans;
}
function checkGoodMatrix(mat) {
let P = [], S = [], MR = [], MC = [];
for (let i = 0; i < mat.length; i++) {
for (let j = 0; j < mat[0].length; j++) {
if (i == j)
P.push(mat[i][j]);
if (i + j == mat.length - 1)
S.push(mat[i][j]);
if (i == Math.floor((mat.length - 1) / 2))
MR.push(mat[i][j]);
if (j == Math.floor((mat.length - 1) / 2))
MC.push(mat[i][j]);
}
}
S.reverse();
let P0 = convert(P);
let S0 = convert(S);
let MR0 = convert(MR);
let MC0 = convert(MC);
let setBitsPS = count((P0 & S0));
let setBitsMM = count((MR0 & MC0));
if (setBitsPS > setBitsMM)
document.write( "Yes" );
else
document.write( "No" );
}
let mat = [[1, 0, 1],
[0, 0, 1],
[0, 1, 1]];
checkGoodMatrix(mat);
</script>
|
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 from (i, i) to P, i.e., main diagonal.
- Append the element from (N – 1 – i, i) to S, i.e., cross diagonal.
- Append the element from ((N-1)/2, i) to MR, i.e., mid-row.
- Append the element from ((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 <bits/stdc++.h>
using namespace std;
void checkGoodMatrix(
vector<vector< int > > M, int N)
{
vector< int > 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 << "Yes" ;
return ;
}
else if (p[i] & s[i] < MR[i] & MC[i]) {
cout << "No" ;
return ;
}
}
cout << "No" ;
}
int main()
{
vector<vector< int > > M{ { 0, 1, 1 },
{ 0, 0, 0 },
{ 0, 1, 1 } };
int N = M.size();
checkGoodMatrix(M, N);
return 0;
}
|
Java
import java.util.Vector;
class GFG{
static void checkGoodMatrix( int [][] M, int N)
{
Vector<Integer> p = new Vector<Integer>();
Vector<Integer> s = new Vector<Integer>();
Vector<Integer> MR = new Vector<Integer>();
Vector<Integer> MC = new Vector<Integer>();
for ( int i = 0 ; i < N; i++)
{
p.add(M[i][i]);
s.add(M[N - 1 - i][i]);
MR.add(M[(N - 1 ) / 2 ][i]);
MC.add(M[i][(N - 1 ) / 2 ]);
}
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" );
}
public static void main(String[] args)
{
int [][] M = { { 0 , 1 , 1 },
{ 0 , 0 , 0 },
{ 0 , 1 , 1 } };
int N = M.length;
checkGoodMatrix(M, N);
}
}
|
Python3
def checkGoodMatrix(M, N):
p = []
s = []
MR = []
MC = []
for i in range (N):
p.append(M[i][i])
s.append(M[N - 1 - i][i])
MR.append(M[(N - 1 ) / / 2 ][i])
MC.append(M[i][(N - 1 ) / / 2 ])
for i in range (N):
if (p[i] & s[i] > MR[i] & MC[i]):
print ( "Yes" )
return
elif (p[i] & s[i] < MR[i] & MC[i]):
print ( "No" )
return
print ( "No" )
M = [ [ 0 , 1 , 1 ],
[ 0 , 0 , 0 ],
[ 0 , 1 , 1 ] ]
N = len (M)
checkGoodMatrix(M, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void checkGoodMatrix( int [,] M, int N)
{
List< int > p = new List< int >();
List< int > s = new List< int >();
List< int > MR = new List< int >();
List< int > MC = new List< int >();
for ( int i = 0; i < N; i++)
{
p.Add(M[i,i]);
s.Add(M[N - 1 - i,i]);
MR.Add(M[(N - 1) / 2,i]);
MC.Add(M[i,(N - 1) / 2]);
}
for ( int i = 0; i < N; i++)
{
int P = p[i];
int S = s[i];
int Mr = MR[i];
int Mc = MC[i];
if ((P & S) > (Mr & Mc))
{
Console.WriteLine( "Yes" );
return ;
}
else if ((P & S) < (Mr & Mc))
{
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "No" );
}
static void Main() {
int [,] M = { { 0, 1, 1 },
{ 0, 0, 0 },
{ 0, 1, 1 } };
int N = 3;
checkGoodMatrix(M, N);
}
}
|
Javascript
<script>
function checkGoodMatrix(M, N)
{
let p = [];
let s = [];
let MR = [];
let MC = [];
for (let i = 0; i < N; i++)
{
p.push(M[i][i]);
s.push(M[N - 1 - i][i]);
MR.push(M[parseInt((N - 1) / 2, 10)][i]);
MC.push(M[i][parseInt((N - 1) / 2, 10)]);
}
for (let i = 0; i < N; i++)
{
let P = p[i];
let S = s[i];
let Mr = MR[i];
let Mc = MC[i];
if ((P & S) > (Mr & Mc))
{
document.write( "Yes" );
return ;
}
else if ((P & S) < (Mr & Mc))
{
document.write( "No" );
return ;
}
}
document.write( "No" );
}
let M = [ [ 0, 1, 1 ],
[ 0, 0, 0 ],
[ 0, 1, 1 ] ];
let N = M.length;
checkGoodMatrix(M, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...