Check if all rows of a Binary Matrix have all ones placed adjacently or not
Last Updated :
16 Aug, 2021
Given a binary matrix mat[][] of dimension N*M, the task is to check if all 1s in each row are placed adjacently on the given matrix. If all 1s in each row are adjacent, then print “Yes”. Otherwise, print “No”.
Examples:
Input: mat[][] = {{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 1}, {1, 1, 1, 0}
Output: Yes
Explanation:
Elements in the first row are {0, 1, 1, 0}.
Elements in the 2nd row are {1, 1, 0, 0}.
Elements in the 3rd row are {0, 0, 0, 1}.
Elements in the 4th row are {1, 1, 1, 0}.
Therefore, all the rows have all 1s grouped together. Therefore, print Yes.
Input: mat[][] = {{1, 0, 1}, {0, 0, 1}, {0, 0, 0}}
Output: No
Approach: The idea is to perform row-wise traversal on the matrix and check if all the 1s in a row are placed adjacently or not by using the property of Bitwise XOR. The given problem can be solved based on the following observations:
- Calculate the sum of Bitwise XOR of every pair of adjacent elements of ith row, say X. All 1s will be not together in the ith row if any of the following conditions are satisfied:
- If X > 2 and mat[i][0] + mat[i][M – 1] = 0.
- If X > 1 and mat[i][0] + mat[i][M – 1] = 1.
- If X > 0 and mat[i][0] + mat[i][M – 1] = 0.
Follow the steps below to solve this problem:
- Traverse the given matrix mat[][] and perform the following operations:
- For each row, check if the value of M is less than 3, then print “Yes”.
- Otherwise, find the sum of Bitwise XOR of adjacent array elements and store it in a variable, say X.
- For every value of X, if any of the above-mentioned conditions holds true, then print “No”.
- After completing the above steps, if any of the above conditions does not hold true for any value of X, then print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkGroup(vector< int > arr)
{
if (arr.size() <= 2)
return true ;
int corner = arr[0] + arr[( int )arr.size()-1];
int xorSum = 0;
for ( int i = 0; i < arr.size() - 1; i++)
xorSum += (arr[i] ^ arr[i + 1]);
if (!corner)
if (xorSum > 2)
return false ;
else if (corner == 1)
if (xorSum > 1)
return false ;
else
if (xorSum > 0)
return false ;
return true ;
}
bool isInGroupUtil(vector<vector< int >> mat)
{
for ( auto i:mat)
{
if (!checkGroup(i))
return false ;
}
return true ;
}
void isInGroup(vector<vector< int >> mat)
{
bool ans = isInGroupUtil(mat);
if (ans)
printf ( "Yes" );
else
printf ( "No" );
}
int main()
{
vector<vector< int >> mat = {{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 1},
{1, 1, 1, 0}};
isInGroup(mat);
}
|
Java
import java.util.*;
public class Main
{
static Boolean checkGroup(Vector<Integer> arr)
{
if (arr.size() <= 2 )
return true ;
int corner = arr.get( 0 ) + arr.get(arr.size()- 1 );
int xorSum = 0 ;
for ( int i = 0 ; i < arr.size() - 1 ; i++)
xorSum += (arr.get(i) ^ arr.get(i + 1 ));
if (corner == 0 )
if (xorSum > 2 )
return false ;
else if (corner == 1 )
if (xorSum > 1 )
return false ;
else
if (xorSum > 0 )
return false ;
return true ;
}
static Boolean isInGroupUtil( int [][] mat)
{
for ( int i = 0 ; i < mat.length; i++)
{
Vector<Integer> arr = new Vector<Integer>();
for ( int j = 0 ; j < mat[i].length; j++)
{
arr.add(mat[i][j]);
}
if (!checkGroup(arr))
return false ;
}
return true ;
}
static void isInGroup( int [][] mat)
{
Boolean ans = isInGroupUtil(mat);
if (ans)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args) {
int [][] mat = {{ 0 , 1 , 1 , 0 },
{ 1 , 1 , 0 , 0 },
{ 0 , 0 , 0 , 1 },
{ 1 , 1 , 1 , 0 }};
isInGroup(mat);
}
}
|
Python3
def checkGroup(arr):
if len (arr) < = 2 :
return True
corner = arr[ 0 ] + arr[ - 1 ]
xorSum = 0
for i in range ( len (arr) - 1 ):
xorSum + = (arr[i] ^ arr[i + 1 ])
if not corner:
if xorSum > 2 :
return False
elif corner = = 1 :
if xorSum > 1 :
return False
else :
if xorSum > 0 :
return False
return True
def isInGroupUtil(mat):
for i in mat:
if not checkGroup(i):
return False
return True
def isInGroup(mat):
ans = isInGroupUtil(mat)
if ans:
print ( "Yes" )
else :
print ( "No" )
mat = [[ 0 , 1 , 1 , 0 ], [ 1 , 1 , 0 , 0 ],
[ 0 , 0 , 0 , 1 ], [ 1 , 1 , 1 , 0 ]]
isInGroup(mat)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool checkGroup(List< int > arr)
{
if (arr.Count <= 2)
return true ;
int corner = arr[0] + arr[arr.Count-1];
int xorSum = 0;
for ( int i = 0; i < arr.Count - 1; i++)
xorSum += (arr[i] ^ arr[i + 1]);
if (corner == 0)
if (xorSum > 2)
return false ;
else if (corner == 1)
if (xorSum > 1)
return false ;
else
if (xorSum > 0)
return false ;
return true ;
}
static bool isInGroupUtil( int [,] mat)
{
for ( int i = 0; i < mat.GetLength(1); i++)
{
List< int > arr = new List< int >();
for ( int j = 0; j < mat.GetLength(0); j++)
{
arr.Add(mat[i,j]);
}
if (!checkGroup(arr))
return false ;
}
return true ;
}
static void isInGroup( int [,] mat)
{
bool ans = isInGroupUtil(mat);
if (ans)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
int [,] mat = {{0, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 1},
{1, 1, 1, 0}};
isInGroup(mat);
}
}
|
Javascript
<script>
function checkGroup(arr)
{
if (arr.length <= 2)
return true ;
let corner = arr[0] + arr[arr.length-1];
let xorSum = 0;
for (let i = 0; i < arr.length - 1; i++)
xorSum += (arr[i] ^ arr[i + 1]);
if (corner == 0)
if (xorSum > 2)
return false ;
else if (corner == 1)
if (xorSum > 1)
return false ;
else
if (xorSum > 0)
return false ;
return true ;
}
function isInGroupUtil(mat)
{
for (let i = 0; i < mat.length; i++)
{
let arr = []
for (let j = 0; j < mat[i].length; j++)
{
arr.push(mat[i][j]);
}
if (!checkGroup(arr))
return false ;
}
return true ;
}
function isInGroup(mat)
{
let ans = isInGroupUtil(mat);
if (ans)
document.write( "Yes" );
else
document.write( "No" );
}
let mat = [[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 1],
[1, 1, 1, 0]];
isInGroup(mat);
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...