Ways to remove one element from a binary string so that XOR becomes zero in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a binary string. Our task is to count the total number of ways in which we can remove one element such that XOR becomes zero.

Let’s take an example to understand the problem,


n = 11010



to solve this problem, we need the logic that if the number of 1’s is even then XOR of the string will be 0, otherwise, we need to remove one 1 from the string. We can remove any number of 0’s without affecting the XOR.

Program to show the implementation of our solution,


 Live Demo

using namespace std;
int wayXorZero(string binaryString){
   int oneCount = 0, zeroCount = 0;
   int n = binaryString.length();
   for (int i = 0; i < n; i++)
      if (binaryString[i] == '1')
   if (oneCount % 2 == 0)
      return zeroCount;
   return oneCount;
int main(){
   string binaryString = "10110100";
   cout<<"Number of ways to make XOR zero is "<<wayXorZero(binaryString);
   return 0;


Number of ways to make XOR zero is 4
Updated on 17-Jul-2020 11:25:29