Given a binary string, task is to erase exactly one integer in the array so that the XOR of the remaining numbers is zero. The task is to count number of ways to remove one element so that XOR of that string become ZERO.

**Examples :**

Input : 10000 Output : 1 We only have 1 ways to Input : 10011 Output : 3 There are 3 ways to make XOR 0. We can remove any of the three 1's. Input : 100011100 Output : 5 There are 5 ways to make XOR 0. We can remove any of the give 0's

A **simple solution **is to one by one remove an element, then compute XOR of remaining string. And count occurrences where removing an element makes XOR 0.

An **efficient solution **is based on following fact. If count of 1s is odd, then we must remove a 1 to make count 0 and we can remove any of the 1s. If count of 1s is even, then XOR is 0, We can remove any of the 0s and XOR will remain 0.

## C++

`// C++ program to count number of ways to ` `// remove an element so that XOR of remaining ` `// string becomes 0. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return number of ways in which XOR become ZERO ` `// by remove 1 element ` `int` `xorZero(string str) ` `{ ` ` ` `int` `one_count = 0, zero_count = 0; ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Counting number of 0 and 1 ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(str[i] == ` `'1'` `) ` ` ` `one_count++; ` ` ` `else` ` ` `zero_count++; ` ` ` ` ` `// If count of ones is even ` ` ` `// then return count of zero ` ` ` `// else count of one ` ` ` `if` `(one_count % 2 == 0) ` ` ` `return` `zero_count; ` ` ` `return` `one_count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string str = ` `"11111"` `; ` ` ` `cout << xorZero(str) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of ways to ` `// remove an element so that XOR of remaining ` `// string becomes 0. ` `import` `java.util.*; ` ` ` `class` `CountWays ` `{ ` ` ` `// Returns number of ways in which XOR become ` ` ` `// ZERO by remove 1 element ` ` ` `static` `int` `xorZero(String s) ` ` ` `{ ` ` ` `int` `one_count = ` `0` `, zero_count = ` `0` `; ` ` ` `char` `[] str=s.toCharArray(); ` ` ` `int` `n = str.length; ` ` ` ` ` `// Counting number of 0 and 1 ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(str[i] == ` `'1'` `) ` ` ` `one_count++; ` ` ` `else` ` ` `zero_count++; ` ` ` ` ` `// If count of ones is even ` ` ` `// then return count of zero ` ` ` `// else count of one ` ` ` `if` `(one_count % ` `2` `== ` `0` `) ` ` ` `return` `zero_count; ` ` ` `return` `one_count; ` ` ` `} ` ` ` ` ` `// Driver Code to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"11111"` `; ` ` ` `System.out.println(xorZero(s)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Mr. Somesh Awasthi ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count number of ` `# ways to remove an element so that ` `# XOR of remaining string becomes 0. ` ` ` `# Return number of ways in which XOR ` `# become ZERO by remove 1 element ` `def` `xorZero(` `str` `): ` ` ` `one_count ` `=` `0` ` ` `zero_count ` `=` `0` ` ` `n ` `=` `len` `(` `str` `) ` ` ` ` ` `# Counting number of 0 and 1 ` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `1` `): ` ` ` `if` `(` `str` `[i] ` `=` `=` `'1'` `): ` ` ` `one_count ` `+` `=` `1` ` ` `else` `: ` ` ` `zero_count ` `+` `=` `1` ` ` ` ` `# If count of ones is even ` ` ` `# then return count of zero ` ` ` `# else count of one ` ` ` `if` `(one_count ` `%` `2` `=` `=` `0` `): ` ` ` `return` `zero_count ` ` ` `return` `one_count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `str` `=` `"11111"` ` ` `print` `(xorZero(` `str` `)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number ` `// of ways to remove an element ` `// so that XOR of remaining ` `// string becomes 0. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns number of ways ` ` ` `// in which XOR become ` ` ` `// ZERO by remove 1 element ` ` ` `static` `int` `xorZero(` `string` `s) ` ` ` `{ ` ` ` `int` `one_count = 0, ` ` ` `zero_count = 0; ` ` ` ` ` `int` `n = s.Length; ` ` ` ` ` `// Counting number of 0 and 1 ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(s[i] == ` `'1'` `) ` ` ` `one_count++; ` ` ` `else` ` ` `zero_count++; ` ` ` ` ` `// If count of ones is even ` ` ` `// then return count of zero ` ` ` `// else count of one ` ` ` `if` `(one_count % 2 == 0) ` ` ` `return` `zero_count; ` ` ` `return` `one_count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `s = ` `"11111"` `; ` ` ` `Console.WriteLine(xorZero(s)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of ways to remove an element ` `// so that XOR of remaining ` `// string becomes 0. ` ` ` `// Return number of ways in ` `// which XOR become ZERO ` `// by remove 1 element ` ` ` `function` `xorZero(` `$str` `) ` `{ ` ` ` `$one_count` `= 0; ` `$zero_count` `= 0; ` ` ` `$n` `= ` `strlen` `(` `$str` `); ` ` ` ` ` `// Counting number of 0 and 1 ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$str` `[` `$i` `] == ` `'1'` `) ` ` ` `$one_count` `++; ` ` ` `else` ` ` `$zero_count` `++; ` ` ` ` ` `// If count of ones is even ` ` ` `// then return count of zero ` ` ` `// else count of one ` ` ` `if` `(` `$one_count` `% 2 == 0) ` ` ` `return` `$zero_count` `; ` ` ` `return` `$one_count` `; ` `} ` ` ` `// Driver Code ` `$str` `= ` `"11111"` `; ` `echo` `xorZero(` `$str` `),` `"\n"` `; ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5

This article is contributed by **Sahil Rajput**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Remove one element to get maximum XOR
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Minimum operations to make XOR of array zero
- Find all possible triangles with XOR of sides zero
- Maximize the number of subarrays with XOR as zero
- Check if the Xor of the frequency of all digits of a number N is zero or not
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Find array such that no subarray has xor zero or Y
- Quadruplet pair with XOR zero in the given Array
- Number of unique triplets whose XOR is zero
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Remove one bit from a binary number to get maximum value
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Different Ways to Remove all the Digits from String in Java
- Ways of transforming one string to other by removing 0 or more characters
- Range Query on array whose each element is XOR of index value and previous element
- Replace every array element by Bitwise Xor of previous and next element