

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Ways to remove one element from a binary string so that XOR becomes zero 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
- Related Questions & Answers
- C++ Program to remove Characters from a Numeric String Such That String Becomes Divisible by 8
- Minimum flips in two binary arrays so that their XOR is equal to another array in C++.
- Add minimum number to an array so that the sum becomes even in C++?
- Find the minimum value to be added so that array becomes balanced in C++
- Add minimum number to an array so that the sum becomes even in C programming
- Remove One Bit from a Binary Number to Get Maximum Value in C++
- Python - Ways to remove a key from dictionary
- Minimum steps to remove substring 010 from a binary string in C++
- Find minimum value to assign all array elements so that array product becomes greater in C++
- How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?
- Python - Ways to remove duplicates from list
- Program to remove all nodes with only one child from a binary tree in Python?
- Design a DFA accepting stringw so that the second symbol is zero and fourth is 1
- Remove new lines from a string and replace with one empty space PHP?
- Print all possible ways to convert one string into another string in C++
Advertisements