Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmCheck if rows of a Matrix can be rearranged to make Bitwise...

Check if rows of a Matrix can be rearranged to make Bitwise XOR of first column non-zero

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if there is any
// row where number of unique elements
// are greater than 1
string checkRearrangements(
    vector<vector<int> > mat, int N, int M)
{
    // Iterate over the matrix
    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";
}
 
// Function to check if it is possible
// to rearrange mat[][] such that XOR
// of its first column is non-zero
string nonZeroXor(vector<vector<int> > mat,
                  int N, int M)
{
    int res = 0;
 
    // Find bitwise XOR of the first
    // column of mat[][]
    for (int i = 0; i < N; i++) {
 
        res = res ^ mat[i][0];
    }
 
    // If bitwise XOR of the first
    // column of mat[][] is non-zero
    if (res != 0)
        return "Yes";
 
    // Otherwise check rearrangements
    else
        return checkRearrangements(mat, N, M);
}
 
// Driver Code
int main()
{
    // Given Matrix mat[][]
    vector<vector<int> > mat
        = { { 1, 1, 2 },
            { 2, 2, 2 },
            { 3, 3, 3 } };
 
    int N = mat.size();
    int M = mat[0].size();
 
    // Function Call
    cout << nonZeroXor(mat, N, M);
 
    return 0;
}


Java




// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to check if there is any
// row where number of unique elements
// are greater than 1
static String checkRearrangements(int[][] mat,
                                  int N, int M)
{
  // Iterate over the matrix
  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";
}
 
// Function to check if it is possible
// to rearrange mat[][] such that XOR
// of its first column is non-zero
static String nonZeroXor(int[][] mat,
                         int N, int M)
{
  int res = 0;
 
  // Find bitwise XOR of the
  // first column of mat[][]
  for (int i = 0; i < N; i++)
  {
    res = res ^ mat[i][0];
  }
 
  // If bitwise XOR of the first
  // column of mat[][] is non-zero
  if (res != 0)
    return "Yes";
 
  // Otherwise check
  // rearrangements
  else
    return checkRearrangements(mat,
                               N, M);
}
 
// Driver Code
public static void main(String[] args)
{
  // Given Matrix mat[][]
  int[][] mat = {{1, 1, 2},
                 {2, 2, 2},
                 {3, 3, 3}};
 
  int N = mat.length;
  int M = mat[0].length;
 
  // Function Call
  System.out.print(nonZeroXor(mat,
                              N, M));
}
}
 
// This code is contributed by gauravrajput1


Python3




# Python3 program for the above approach
 
# Function to check if there is any
# row where number of unique elements
# are greater than 1
def checkRearrangements(mat, N, M):
     
    # Iterate over the matrix
    for i in range(N):
        for j in range(1, M):
            if (mat[i][0] != mat[i][j]):
                return "Yes"
                 
    return "No"
 
# Function to check if it is possible
# to rearrange mat[][] such that XOR
# of its first column is non-zero
def nonZeroXor(mat, N, M):
 
    res = 0
 
    # Find bitwise XOR of the first
    # column of mat[][]
    for i in range(N):
        res = res ^ mat[i][0]
 
    # If bitwise XOR of the first
    # column of mat[][] is non-zero
    if (res != 0):
        return "Yes"
 
    # Otherwise check rearrangements
    else:
        return checkRearrangements(mat, N, M)
 
# Driver Code
if __name__ == "__main__":
 
    # Given Matrix mat[][]
    mat = [ [ 1, 1, 2 ],
            [ 2, 2, 2 ],
            [ 3, 3, 3 ] ]
 
    N = len(mat)
    M = len(mat[0])
 
    # Function Call
    print(nonZeroXor(mat, N, M))
 
# This code is contributed by chitranayal


C#




// C# program for the
// above approach
using System;
 
class GFG{
 
// Function to check if there is any
// row where number of unique elements
// are greater than 1
static String checkRearrangements(int[,] mat,
                                  int N, int M)
{
   
  // Iterate over the matrix
  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";
}
 
// Function to check if it is possible
// to rearrange [,]mat such that XOR
// of its first column is non-zero
static String nonZeroXor(int[,] mat,
                         int N, int M)
{
  int res = 0;
 
  // Find bitwise XOR of the
  // first column of [,]mat
  for(int i = 0; i < N; i++)
  {
    res = res ^ mat[i, 0];
  }
 
  // If bitwise XOR of the first
  // column of [,]mat is non-zero
  if (res != 0)
    return "Yes";
 
  // Otherwise check
  // rearrangements
  else
    return checkRearrangements(mat,
                               N, M);
}
 
// Driver Code
public static void Main(String[] args)
{
   
  // Given Matrix [,]mat
  int[,] mat = { { 1, 1, 2 },
                 { 2, 2, 2 },
                 { 3, 3, 3 } };
 
  int N = mat.GetLength(0);
  int M = mat.GetLength(1);
 
  // Function Call
  Console.Write(nonZeroXor(mat,
                           N, M));
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to check if there is any
// row where number of unique elements
// are greater than 1
function checkRearrangements(mat, N, M)
{
  // Iterate over the matrix
  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 to check if it is possible
// to rearrange mat[][] such that XOR
// of its first column is non-zero
function nonZeroXor(mat, N, M)
{
  let res = 0;
  
  // Find bitwise XOR of the
  // first column of mat[][]
  for (let i = 0; i < N; i++)
  {
    res = res ^ mat[i][0];
  }
  
  // If bitwise XOR of the first
  // column of mat[][] is non-zero
  if (res != 0)
    return "Yes";
  
  // Otherwise check
  // rearrangements
  else
    return checkRearrangements(mat,
                               N, M);
}
 
// Driver Code
 
    // Given Matrix mat[][]
  let mat = [[1, 1, 2],
             [2, 2, 2],
             [3, 3, 3]];
  
  let N = mat.length;
  let M = mat[0].length;
  
  // Function Call
  document.write(nonZeroXor(mat,
                              N, M));
           
</script>


Output: 

Yes

 

Time Complexity: O(N * M)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments