给定一个有序NXM数组元素的二维数组,任务是检查我们是否可以从每一行中选择一个数字,使所选数字的xor大于0 。
注意:至少有2行。
例子:
Input: a[][] = {{7, 7, 7},
{10, 10, 7}}
Output: Yes
Input: a[][] = {{1, 1, 1},
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}}
Output: No
方法:最初检查每行的第一列元素的xor是否为0。如果它不为零,则有可能。如果它为零,请检查是否有任何行具有两个或更多不同的元素,那么这也是可能的。如果以上两个条件均不满足,则不可能。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
#define N 2
#define M 3
// Function to check if a number from every row
// can be selected such that xor of the numbers
// is greater than zero
bool check(int mat[N][M])
{
int xorr = 0;
// Find the xor of first
// column for every row
for (int i = 0; i < N; i++) {
xorr ^= mat[i][0];
}
// If Xorr is 0
if (xorr != 0)
return true;
// Traverse in the matrix
for (int i = 0; i < N; i++) {
for (int j = 1; j < M; j++) {
// Check is atleast
// 2 distinct elements
if (mat[i][j] != mat[i][0])
return true;
}
}
return false;
}
// Driver code
int main()
{
int mat[N][M] = { { 7, 7, 7 },
{ 10, 10, 7 } };
if (check(mat))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG
{
static int N = 2;
static int M = 3;
// Function to check if a number
// from every row can be selected
// such that xor of the numbers
// is greater than zero
static boolean check(int mat[][])
{
int xorr = 0;
// Find the xor of first
// column for every row
for (int i = 0; i < N; i++)
{
xorr ^= mat[i] [0];
}
// If Xorr is 0
if (xorr != 0)
return true;
// Traverse in the matrix
for (int i = 0; i < N; i++)
{
for (int j = 1; j < M; j++)
{
// Check is atleast
// 2 distinct elements
if (mat[i] [j] != mat[i] [0])
return true;
}
}
return false;
}
// Driver code
public static void main (String[] args)
{
int mat[][] = {{ 7, 7, 7 },
{ 10, 10, 7 }};
if (check(mat))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by ajit
Python3
# Python3 program to implement
# the above approach
N = 2
M = 3
# Function to check if a number from every row
# can be selected such that xor of the numbers
# is greater than zero
def check(mat):
xorr = 0
# Find the xor of first
# column for every row
for i in range(N):
xorr ^= mat[i][0]
# If Xorr is 0
if (xorr != 0):
return True
# Traverse in the matrix
for i in range(N):
for j in range(1, M):
# Check is atleast
# 2 distinct elements
if (mat[i][j] != mat[i][0]):
return True
return False
# Driver code
mat = [[ 7, 7, 7 ],
[ 10, 10, 7 ]]
if (check(mat)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
static int N = 2;
static int M = 3;
// Function to check if a number
// from every row can be selected
// such that xor of the numbers
// is greater than zero
static bool check(int [,]mat)
{
int xorr = 0;
// Find the xor of first
// column for every row
for (int i = 0; i < N; i++)
{
xorr ^= mat[i, 0];
}
// If Xorr is 0
if (xorr != 0)
return true;
// Traverse in the matrix
for (int i = 0; i < N; i++)
{
for (int j = 1; j < M; j++)
{
// Check is atleast
// 2 distinct elements
if (mat[i, j] != mat[i, 0])
return true;
}
}
return false;
}
// Driver code
static void Main()
{
int [,]mat = {{ 7, 7, 7 },
{ 10, 10, 7 }};
if (check(mat))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by mits
PHP
输出:
Yes