C++ program to check xor game results 0 or not

Suppose we have an array A with N elements and another binary string S. Consider two players are playing a game. They are numbered as 0 and 1. There is one variable x whose initial value is 0. The games has N rounds. In ith round person S[i] does one of the following: replace x with x XOR A[i], otherwise do nothing. Person 0 wants 0 at the end of this game but person 1 wants non-zero. We have to check whether x becomes 0 at the end or not.

So, if the input is like A = [1, 2]; S = "10", then the output will be 1, because person1 changes x with 0 XOR 1 = 1, so it will always be 1 regardless the choice by person0.

Steps

To solve this, we will follow these steps −

N := size of A
Define an array judge of size: 60.
z := 0
fill judge with 0
for initialize n := N - 1, when 0 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;

int solve(vector A, string S){
   int N = A.size();
   int judge[60];
   int z = 0;
   fill(judge, judge + 60, 0);
   for (int n = N - 1; 0  A = { 1, 2 };
   string S = "10";
   cout 

Input

{ 1, 2 }, "10"

Output

1
Updated on: 2022-03-03T07:56:00+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements