# Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element

Given an array arr[] of length N, the task is to find the count of array indices such that removing an element from these indices makes the Bitwise xor of odd-indexed elements and even-indexed (1-based indexing) elements are equal.

Examples:

Input: arr[] = {1, 0, 1, 0, 1}, N = 5
Output: 3
Explanation:

1. Removing an element from index 3 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
2. Removing an element from index 1 modifies arr[] to {0, 1, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
3. Removing an element from index 5 modifies arr[] to {1, 0, 1, 0}. Therefore, xor of odd and even indexed elements is 0.

Input: arr[] = {1, 0, 0, 0, 1}, N=5
Output:

1. Removing an element from index 3 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
2. Removing an element from index 2 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 1.
3. Removing an element from index 4 modifies arr[] to {1, 0, 0, 0}. Therefore, xor of odd and even indexed elements is 1.

Naive Approach: The simplest approach to solve this problem is to traverse the array and for each array element, check if removing the element from the array makes the Bitwise XOR of even-indexed and odd-indexed array elements is equal or not. If found to be true, then increment the count. Finally, print the count.

Time Complexity: O(N2
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the observation that removing any element from the given array makes even indices of succeeding elements as odd and odd indices of the succeeding elements as even. Follow the steps below to solve the problem:

1. Initialize variables curr_odd, curr_even, post_odd, post_even, and res with 0.
2. Traverse the array in reverse and perform the following:
• If current element is odd, XOR with post_odd.
• Otherwise, XOR current element with post_even.
3. Now, traverse the given array and perform the following:
• If current index is odd, then remove the current element from post_odd by updating post_odd with Bitwise XOR of post_odd and current element.
• Otherwise, remove the current element from post_even similarly.
• Initialize variables X and Y.
• Assign XOR of curr_odd and post_even in X. Therefore, X stores xor of all odd-indexed elements.
• Assign xor of curr_even and post_odd in Y. Therefore, Y stores xor of all even-indexed elements.
• Check if X is equal to Y. If found to be true, increment res by 1.
• If current index is odd, then XOR current element with curr_odd.
• Otherwise, XOR current element with curr_even.
4. Finally, print the res.

Below is the implementation of the above approach:

 // C++ program for the above approach   #include using namespace std;   // Function to count ways to make Bitwise // XOR of odd and even indexed elements // equal by removing an array element void Remove_one_element(int arr[], int n) {     // Stores xor of odd and even     // indexed elements from the end     int post_odd = 0, post_even = 0;       // Stores xor of odd and even     // indexed elements from the start     int curr_odd = 0, curr_even = 0;       // Stores the required count     int res = 0;       // Traverse the array in reverse     for (int i = n - 1; i >= 0; i--) {           // If i is odd         if (i % 2)             post_odd ^= arr[i];           // If i is even         else             post_even ^= arr[i];     }       // Traverse the array     for (int i = 0; i < n; i++) {           // If i is odd         if (i % 2)             post_odd ^= arr[i];           // If i is even         else             post_even ^= arr[i];           // Removing arr[i], post_even stores         // XOR of odd indexed elements         int X = curr_odd ^ post_even;           // Removing arr[i], post_odd stores         // XOR of even indexed elements         int Y = curr_even ^ post_odd;           // Check if they are equal         if (X == Y)             res++;           // If i is odd, xor it         // with curr_odd         if (i % 2)             curr_odd ^= arr[i];           // If i is even, xor it         // with curr_even         else             curr_even ^= arr[i];     }       // Finally print res     cout << res << endl; }   // Drivers Code int main() {       // Given array     int arr[] = { 1, 0, 1, 0, 1 };       // Given size     int N = sizeof(arr) / sizeof(arr[0]);       // Function call     Remove_one_element(arr, N);       return 0; }

 // Java program for the above approach class GFG {           // Function to count ways to make Bitwise     // XOR of odd and even indexed elements     // equal by removing an array element     static void Remove_one_element(int arr[], int n)     {         // Stores xor of odd and even         // indexed elements from the end         int post_odd = 0, post_even = 0;               // Stores xor of odd and even         // indexed elements from the start         int curr_odd = 0, curr_even = 0;               // Stores the required count         int res = 0;               // Traverse the array in reverse         for (int i = n - 1; i >= 0; i--)         {                   // If i is odd             if (i % 2 != 0)                 post_odd ^= arr[i];                   // If i is even             else                 post_even ^= arr[i];         }               // Traverse the array         for (int i = 0; i < n; i++)         {                   // If i is odd             if (i % 2 != 0)                 post_odd ^= arr[i];                   // If i is even             else                 post_even ^= arr[i];                   // Removing arr[i], post_even stores             // XOR of odd indexed elements             int X = curr_odd ^ post_even;                   // Removing arr[i], post_odd stores             // XOR of even indexed elements             int Y = curr_even ^ post_odd;                   // Check if they are equal             if (X == Y)                 res++;                   // If i is odd, xor it             // with curr_odd             if (i % 2 != 0)                 curr_odd ^= arr[i];                   // If i is even, xor it             // with curr_even             else                 curr_even ^= arr[i];         }               // Finally print res         System.out.println(res);     }           // Drivers Code     public static void main (String[] args)     {               // Given array         int arr[] = { 1, 0, 1, 0, 1 };               // Given size         int N = arr.length;               // Function call         Remove_one_element(arr, N);        }   }   // This code is contributed by AnkitRai01

 # Python3 program for the above approach   # Function to count ways to make Bitwise # XOR of odd and even indexed elements # equal by removing an array element def Remove_one_element(arr, n):       # Stores xor of odd and even     # indexed elements from the end     post_odd = 0     post_even = 0       # Stores xor of odd and even     # indexed elements from the start     curr_odd = 0     curr_even = 0       # Stores the required count     res = 0       # Traverse the array in reverse     for i in range(n - 1, -1, -1):           # If i is odd         if (i % 2):             post_odd ^= arr[i]           # If i is even         else:             post_even ^= arr[i]       # Traverse the array     for i in range(n):           # If i is odd         if (i % 2):             post_odd ^= arr[i]           # If i is even         else:             post_even ^= arr[i]           # Removing arr[i], post_even stores         # XOR of odd indexed elements         X = curr_odd ^ post_even           # Removing arr[i], post_odd stores         # XOR of even indexed elements         Y = curr_even ^ post_odd           # Check if they are equal         if (X == Y):             res += 1           # If i is odd, xor it         # with curr_odd         if (i % 2):             curr_odd ^= arr[i]           # If i is even, xor it         # with curr_even         else:             curr_even ^= arr[i]       # Finally print res     print(res)   # Drivers Code if __name__ == "__main__" :       # Given array     arr = [ 1, 0, 1, 0, 1 ]       # Given size     N = len(arr)       # Function call     Remove_one_element(arr, N)   # This code is contributed by AnkitRai01

 // C# program to implement // the above approach  using System;   class GFG {            // Function to count ways to make Bitwise     // XOR of odd and even indexed elements     // equal by removing an array element     static void Remove_one_element(int[] arr, int n)     {         // Stores xor of odd and even         // indexed elements from the end         int post_odd = 0, post_even = 0;                // Stores xor of odd and even         // indexed elements from the start         int curr_odd = 0, curr_even = 0;                // Stores the required count         int res = 0;                // Traverse the array in reverse         for (int i = n - 1; i >= 0; i--)         {                    // If i is odd             if (i % 2 != 0)                 post_odd ^= arr[i];                    // If i is even             else                 post_even ^= arr[i];         }                // Traverse the array         for (int i = 0; i < n; i++)         {                    // If i is odd             if (i % 2 != 0)                 post_odd ^= arr[i];                    // If i is even             else                 post_even ^= arr[i];                    // Removing arr[i], post_even stores             // XOR of odd indexed elements             int X = curr_odd ^ post_even;                    // Removing arr[i], post_odd stores             // XOR of even indexed elements             int Y = curr_even ^ post_odd;                    // Check if they are equal             if (X == Y)                 res++;                    // If i is odd, xor it             // with curr_odd             if (i % 2 != 0)                 curr_odd ^= arr[i];                    // If i is even, xor it             // with curr_even             else                 curr_even ^= arr[i];         }                // Finally print res         Console.WriteLine(res);     }            // Drivers Code     public static void Main ()     {                // Given array         int[] arr = { 1, 0, 1, 0, 1 };                // Given size         int N = arr.Length;                // Function call         Remove_one_element(arr, N);        }    }   // This code is contributed by susmitakundugoaldanga

Output:
3

Time Complexity: O(N)
Auxiliary Space: 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.