Horje
XOR Folding of a Grid

You are given a n*m grid containing 0s and 1s. In this grid-folding operation, you repeatedly choose any horizontal or vertical line to fold the grid along, replacing overlapping cell pairs with their XOR. This process continues until you have a 1×1 grid. Check if it’s possible to perform a series of folds on the grid so that the final 1×1 cell contains the value 1. Output “YES” if possible or “NO” otherwise.

Examples:

Input: {{010},
{101},
{010}}
Output: “NO”
Explanation: 1.If we fold 1st horizontal line, grid becomes {{1,1,1},{0,1,0}}
2.Again folding 1st horizontal line, grid becomes {{1,0,1}}
3.After folding 2nd vertical line , grid is {{1,1}} which is followed by 1st vertical line folding and finally grid becomes 0 .In this example it is impossible to make grid as 1 .

Input: {{1,1},{1,0}}
Output: “YES”
Explanation:1.Fold 1st horizontal line make grid as {{0,1}}
2.Fold along 1st vertical line make grid 1
It is possible to make 1.

Approach: To solve the problem follow the below idea.

  • The main observation is to find that the final digit will be the XOR of all the digits present in the grid. Whatever folding we do in any sequence, ultimately operations are just doing XOR of digits.
  • Now how can we achieve 1 in the end?
  • If the number of 1s in the grid is odd, then only the final output can be 1 otherwise 0.

Steps to implement the Idea:

  • Iterate through the grid and count the number of 1s in the grid.
  • If count is even , it is impossible to get 1 .Hence output “NO
  • If count is odd, output “YES

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>
using namespace std;
 
// Method to find XOR of all digits
void XOR_Folder(vector<string>& A)
{
    int n = A.size();
    int m = A[0].size();
 
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (A[i][j] == '1') {
                count++;
            }
        }
    }
    if (count % 2 == 0) {
        cout << "NO" << endl;
    }
    else {
        cout << "YES" << endl;
    }
}
int main()
{
 
    // Inputs
    vector<string> A = { "000", "010", "110" };
 
    // Function call
    XOR_Folder(A);
 
    return 0;
}

Java

import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Inputs
        List<String> A = Arrays.asList("000", "010", "110");
        // Function call
        XOR_Folder(A);
    }
    // Method to find XOR of all digits
    public static void XOR_Folder(List<String> A)
    {
        int n = A.size();
        int m = A.get(0).length();
 
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (A.get(i).charAt(j) == '1') {
                    count++;
                }
            }
        }
        if (count % 2 == 0) {
            System.out.println("NO");
        }
        else {
            System.out.println("YES");
        }
    }
}

Python3

def XOR_folder(A):
    n = len(A)
    m = len(A[0])
 
    count = 0
    for i in range(n):
        for j in range(m):
            if A[i][j] == '1':
                count += 1
 
    if count % 2 == 0:
        print("NO")
    else:
        print("YES")
 
 
if __name__ == "__main__":
    # Inputs
    A = ["000", "010", "110"]
 
    # Function call
    XOR_folder(A)
 
# This code is contributed by shivamgupta0987654321

C#

using System;
 
public class GFG {
    // Method to find XOR of all digits
    static void XOR_Folder(string[] A)
    {
        int n = A.Length;
        int m = A[0].Length;
 
        int count = 0;
        foreach(string s in A)
        {
            foreach(char c in s)
            {
                if (c == '1') {
                    count++;
                }
            }
        }
        if (count % 2 == 0) {
            Console.WriteLine("NO");
        }
        else {
            Console.WriteLine("YES");
        }
    }
 
    static public void Main()
    {
        // Inputs
        string[] A = { "000", "010", "110" };
 
        // Function call
        XOR_Folder(A);
    }
}

Javascript

// Method to find XOR of all digits
function XOR_Folder(A) {
    const n = A.length;
    const m = A[0].length;
 
    let count = 0;
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            if (A[i][j] === '1') {
                count++;
            }
        }
    }
    if (count % 2 === 0) {
        console.log("NO");
    } else {
        console.log("YES");
    }
}
 
// Inputs
const A = ["000", "010", "110"];
 
// Function call
XOR_Folder(A);

Output

YES










Time Complexity: O(n*m)
Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Bit Magic

Related
Solving Binary String Modulo Problem Solving Binary String Modulo Problem
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal
Find Minimum Bitwise XOR By Removing Atmost One Element Find Minimum Bitwise XOR By Removing Atmost One Element
Maximum Strings Concatenation Maximum Strings Concatenation
XOR Graph Minimum Spanning Tree XOR Graph Minimum Spanning Tree

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14