Check if a number from every row can be selected such that xor of the numbers is greater than zero

Given a 2-D array of order N X M array elements, the task is to check if we can select a number from every row in such a way that xor of the selected numbers is greater than 0.

Note: There is a minimum of 2 rows.

Examples:



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 

Approach: Initially check if xor of first column elements of every row is 0 or not. If it is non-zero then it is possible. If it is zero, check if any of the rows has two or more distinct elements, then also it is possible. If both of the above conditions are not satisfied, then it is not possible.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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
function check($mat)
{
      
    global $N ;
    global $M ;
  
    $xorr = 0;
  
    // Find the xor of first
    // column for every row
    for ($i = 0; $i < $N; $i++) 
    {
        $xorr =     $xorr ^ $mat[$i][0];
    }
  
    // If Xorr is 0
    if ($xorr != 0)
        return true;
  
    // Traverse in the matrix
    for ($i = 0; $i < $N; $i++)
    {
        for ( $j = 1; $j < $M; $j++)
        {
  
            // Check is atleast
            // 2 distinct elements
            if ($mat[$i][$j] != $mat[$i][0])
                return true;
        }
    }
    return false;
}
  
    // Driver code
    $mat = array(array( 7, 7, 7 ),
                    array( 10, 10, 7 ));
  
    if (check($mat))
        echo "Yes";
    else
        echo "No";
  
// This code is contributed by Tushil..
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.