Given a binary array of 0’s and 1’s, the task is to find the number of ways to erase exactly one element from this array to make XOR zero.

**Examples:**

Input:arr = {1, 1, 1, 1, 1 }Output:5 You can erase any of the given 5 1's, it will make the XOR of the rest equal to zero.Input:arr = {1, 0, 0, 1, 0 }Output:3 Since the XOR of array is already 0, You can erase any of the given 3 0's so that the XOR remains unaffected.

**Approach:** Since we know that, to make XOR of binary elements be 0, the number of 1’s should be even. Hence this problem can be broken into 4 cases:

**When the number of 1’s is even and the number of 0’s is also even in the given array:**In this scenario, the XOR of the array is already 0. Hence to keep the XOR unaffected, we can remove only the 0’s. Hence the number of ways to erase exactly one element from this array to make XOR zero is the**number of 0’s**in this array.**When the number of 1’s is even and the number of 0’s is odd in the given array:**In this scenario, the XOR of the array is already 0. Hence to keep the XOR unaffected, we can remove only the 0’s. Hence the number of ways to erase exactly one element from this array to make XOR zero is the**number of 0’s**in this array.**When the number of 1’s is odd and the number of 0’s is even in the given array:**In this scenario, the XOR of the array is 1. Hence to make the XOR 0, we can remove any of the 1’s. Hence the number of ways to erase exactly one element from this array to make XOR zero is the**number of 1’s**in this array.**When the number of 1’s is odd and the number of 0’s is also odd in the given array:**In this scenario, the XOR of the array is 1. Hence to make the XOR 0, we can remove any of the 1’s. Hence the number of ways to erase exactly one element from this array to make XOR zero is the**number of 1’s**in this array.

Below is the implementation of the above approach:

## C++

`// C++ program to find the number of ways ` `// to erase exactly one element ` `// from this array to make XOR zero ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of ways ` `int` `no_of_ways(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `count_0 = 0, count_1 = 0; ` ` ` ` ` `// Calculate the number of 1's and 0's ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(a[i] == 0) ` ` ` `count_0++; ` ` ` `else` ` ` `count_1++; ` ` ` `} ` ` ` ` ` `// Considering the 4 cases ` ` ` `if` `(count_1 % 2 == 0) ` ` ` `return` `count_0; ` ` ` `else` ` ` `return` `count_1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `a1[4] = { 1, 1, 0, 0 }; ` ` ` `cout << no_of_ways(a1, n) << endl; ` ` ` ` ` `n = 5; ` ` ` `int` `a2[5] = { 1, 1, 1, 0, 0 }; ` ` ` `cout << no_of_ways(a2, n) << endl; ` ` ` ` ` `n = 5; ` ` ` `int` `a3[5] = { 1, 1, 0, 0, 0 }; ` ` ` `cout << no_of_ways(a3, n) << endl; ` ` ` ` ` `n = 6; ` ` ` `int` `a4[6] = { 1, 1, 1, 0, 0, 0 }; ` ` ` `cout << no_of_ways(a4, n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of ways ` `// to erase exactly one element ` `// from this array to make XOR zero ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the number of ways ` ` ` `static` `int` `no_of_ways(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` `int` `count_0 = ` `0` `, count_1 = ` `0` `; ` ` ` ` ` `// Calculate the number of 1's and 0's ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] == ` `0` `) ` ` ` `count_0++; ` ` ` `else` ` ` `count_1++; ` ` ` `} ` ` ` ` ` `// Considering the 4 cases ` ` ` `if` `(count_1 % ` `2` `== ` `0` `) ` ` ` `return` `count_0; ` ` ` `else` ` ` `return` `count_1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `int` `a1[] = { ` `1` `, ` `1` `, ` `0` `, ` `0` `}; ` ` ` `System.out.println(no_of_ways(a1, n)); ` ` ` ` ` `n = ` `5` `; ` ` ` `int` `a2[] = { ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `}; ` ` ` `System.out.println(no_of_ways(a2, n)); ` ` ` ` ` `n = ` `5` `; ` ` ` `int` `a3[] = { ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `0` `}; ` ` ` `System.out.println(no_of_ways(a3, n)); ` ` ` ` ` `n = ` `6` `; ` ` ` `int` `a4[] = { ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `0` `}; ` ` ` `System.out.println(no_of_ways(a4, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the number of ways ` `# to erase exactly one element ` `# from this array to make XOR zero ` ` ` `# Function to find the number of ways ` `def` `no_of_ways(a, n): ` ` ` ` ` `count_0 ` `=` `0` ` ` `count_1 ` `=` `0` ` ` ` ` `# Calculate the number of 1's and 0's ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `if` `(a[i] ` `=` `=` `0` `): ` ` ` `count_0 ` `+` `=` `1` ` ` `else` `: ` ` ` `count_1 ` `+` `=` `1` ` ` ` ` `# Considering the 4 cases ` ` ` `if` `(count_1 ` `%` `2` `=` `=` `0` `): ` ` ` `return` `count_0 ` ` ` `else` `: ` ` ` `return` `count_1 ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `4` ` ` `a1 ` `=` `[ ` `1` `, ` `1` `, ` `0` `, ` `0` `] ` ` ` `print` `(no_of_ways(a1, n)) ` ` ` ` ` `n ` `=` `5` ` ` `a2 ` `=` `[ ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `] ` ` ` `print` `(no_of_ways(a2, n)) ` ` ` ` ` `n ` `=` `5` ` ` `a3 ` `=` `[ ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `0` `] ` ` ` `print` `(no_of_ways(a3, n)) ` ` ` ` ` `n ` `=` `6` ` ` `a4 ` `=` `[ ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `0` `] ` ` ` `print` `(no_of_ways(a4, n)) ` ` ` `# This code is contributed by ashutosh450 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of ways ` `// to erase exactly one element ` `// from this array to make XOR zero ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the number of ways ` ` ` `static` `int` `no_of_ways(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `int` `count_0 = 0, count_1 = 0; ` ` ` ` ` `// Calculate the number of 1's and 0's ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(a[i] == 0) ` ` ` `count_0++; ` ` ` `else` ` ` `count_1++; ` ` ` `} ` ` ` ` ` `// Considering the 4 cases ` ` ` `if` `(count_1 % 2 == 0) ` ` ` `return` `count_0; ` ` ` `else` ` ` `return` `count_1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `int` `[] a1 = { 1, 1, 0, 0 }; ` ` ` `Console.WriteLine(no_of_ways(a1, n)); ` ` ` ` ` `n = 5; ` ` ` `int` `[] a2 = { 1, 1, 1, 0, 0 }; ` ` ` `Console.WriteLine(no_of_ways(a2, n)); ` ` ` ` ` `n = 5; ` ` ` `int` `[] a3 = { 1, 1, 0, 0, 0 }; ` ` ` `Console.WriteLine(no_of_ways(a3, n)); ` ` ` ` ` `n = 6; ` ` ` `int` `[] a4 = { 1, 1, 1, 0, 0, 0 }; ` ` ` `Console.WriteLine(no_of_ways(a4, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Mohit kumar ` |

*chevron_right*

*filter_none*

**Output:**

2 3 3 3

**Time Complexity:** O(n)

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:

- Count of ways to make Array sum even by removing only one element
- Number of ways to change the Array such that largest element is LCM of array
- Smallest number to make Array sum at most K by dividing each element
- Minimum number of moves to make a binary array K periodic
- Ways to remove one element from a binary string so that XOR becomes zero
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Number of ways to split a binary number such that every part is divisible by 2
- Minimum array element changes to make its elements 1 to N
- Change one element in the given array to make it an Arithmetic Progression
- Minimum changes required to make all element in an array equal
- Minimum pair sum operations to make array each element divisible by 4
- Minimize operations required to make each element of Array equal to it's index value
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Minimum Group Flips to Make Binary Array Elements Same
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Minimum number of replacements to make the binary string alternating | Set 2
- Number of ways to select exactly K even numbers from given Array
- Count number of ways to divide an array into two halves with same sum
- Number of ways to choose elements from the array such that their average is K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.