
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove One Element from Binary String for Zero XOR in C++
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,
Input
n = 11010
Output
3
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,
Example
#include<iostream> #include<string.h> 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') oneCount++; else zeroCount++; 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; }
Output
Number of ways to make XOR zero is 4
Advertisements