给定大小为N * M的矩阵mat [] [] ,任务是检查是否可以重新排列矩阵的行元素,以使第一列元素的按位XOR不为零。如果可能,则打印“是”,否则打印“否” 。
例子:
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.
方法:请按照以下步骤解决问题:
- 查找矩阵第一列元素的按位XOR并将其存储在变量res中。
- 如果res非零,则打印“是” 。
- 否则,遍历所有行并在一行中找到不等于该行第一个索引处的元素的元素。
- 如果在上述步骤的任何行中都不存在此类元素,则打印“否”,否则打印“是”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if there is any
// row where number of unique elements
// are greater than 1
string checkRearrangements(
vector > 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 > 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 > 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
输出:
Yes
时间复杂度: O(N * M)
辅助空间: O(1)