Check if rows of a Matrix can be rearranged to make Bitwise XOR of first column non-zero
Last Updated :
23 Apr, 2021
Given a matrix mat[][] of size N * M, the task is to check if it is possible to rearrange the row elements of the matrix such that Bitwise XOR of the first column element is non-zero. If it is possible then print “Yes” else print “No”.
Examples:
Input: mat[][] = {{1, 1, 2}, {2, 2, 2}, {3, 3, 3}}
Output: Yes
Explanation:
After rearranging the first row as 2, 1, 1.
Bitwise XOR of the first column will be 3 i.e., (2 ^ 2 ^ 3).
Input: mat[][] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}
Output: No
Explanation:
As all the rearrangements give same first element so the only combination is (1 ^ 2 ^ 3) which equals zero.
Therefore, it is not possible to obtain non-zero Bitwise XOR of the first column.
Approach: Follow the steps below to solve the problem:
- Find the Bitwise XOR of the elements of the first column of the matrix and store it in a variable res.
- If res is non-zero then print “Yes”.
- Else, iterate over all the rows and find the element in a row which is not equal to the element at the first index of this row.
- If no such element is present in any row in the above step then print “No” else then print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string checkRearrangements(
vector<vector< int > > mat, int N, int M)
{
for ( int i = 0; i < N; i++) {
for ( int j = 1; j < M; j++) {
if (mat[i][0] != mat[i][j]) {
return "Yes" ;
}
}
}
return "No" ;
}
string nonZeroXor(vector<vector< int > > mat,
int N, int M)
{
int res = 0;
for ( int i = 0; i < N; i++) {
res = res ^ mat[i][0];
}
if (res != 0)
return "Yes" ;
else
return checkRearrangements(mat, N, M);
}
int main()
{
vector<vector< int > > mat
= { { 1, 1, 2 },
{ 2, 2, 2 },
{ 3, 3, 3 } };
int N = mat.size();
int M = mat[0].size();
cout << nonZeroXor(mat, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String checkRearrangements( int [][] mat,
int N, int M)
{
for ( int i = 0 ; i < N; i++)
{
for ( int j = 1 ; j < M; j++)
{
if (mat[i][ 0 ] != mat[i][j])
{
return "Yes" ;
}
}
}
return "No" ;
}
static String nonZeroXor( int [][] mat,
int N, int M)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
res = res ^ mat[i][ 0 ];
}
if (res != 0 )
return "Yes" ;
else
return checkRearrangements(mat,
N, M);
}
public static void main(String[] args)
{
int [][] mat = {{ 1 , 1 , 2 },
{ 2 , 2 , 2 },
{ 3 , 3 , 3 }};
int N = mat.length;
int M = mat[ 0 ].length;
System.out.print(nonZeroXor(mat,
N, M));
}
}
|
Python3
def checkRearrangements(mat, N, M):
for i in range (N):
for j in range ( 1 , M):
if (mat[i][ 0 ] ! = mat[i][j]):
return "Yes"
return "No"
def nonZeroXor(mat, N, M):
res = 0
for i in range (N):
res = res ^ mat[i][ 0 ]
if (res ! = 0 ):
return "Yes"
else :
return checkRearrangements(mat, N, M)
if __name__ = = "__main__" :
mat = [ [ 1 , 1 , 2 ],
[ 2 , 2 , 2 ],
[ 3 , 3 , 3 ] ]
N = len (mat)
M = len (mat[ 0 ])
print (nonZeroXor(mat, N, M))
|
C#
using System;
class GFG{
static String checkRearrangements( int [,] mat,
int N, int M)
{
for ( int i = 0; i < N; i++)
{
for ( int j = 1; j < M; j++)
{
if (mat[i, 0] != mat[i, j])
{
return "Yes" ;
}
}
}
return "No" ;
}
static String nonZeroXor( int [,] mat,
int N, int M)
{
int res = 0;
for ( int i = 0; i < N; i++)
{
res = res ^ mat[i, 0];
}
if (res != 0)
return "Yes" ;
else
return checkRearrangements(mat,
N, M);
}
public static void Main(String[] args)
{
int [,] mat = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 3, 3, 3 } };
int N = mat.GetLength(0);
int M = mat.GetLength(1);
Console.Write(nonZeroXor(mat,
N, M));
}
}
|
Javascript
<script>
function checkRearrangements(mat, N, M)
{
for (let i = 0; i < N; i++)
{
for (let j = 1; j < M; j++)
{
if (mat[i][0] != mat[i][j])
{
return "Yes" ;
}
}
}
return "No" ;
}
function nonZeroXor(mat, N, M)
{
let res = 0;
for (let i = 0; i < N; i++)
{
res = res ^ mat[i][0];
}
if (res != 0)
return "Yes" ;
else
return checkRearrangements(mat,
N, M);
}
let mat = [[1, 1, 2],
[2, 2, 2],
[3, 3, 3]];
let N = mat.length;
let M = mat[0].length;
document.write(nonZeroXor(mat,
N, M));
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...