Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 01 Jun, 2021

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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




// 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




# 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#




// 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

Javascript




<script>
// JavaScript program to implement
// the above approach
 
    // Function to count ways to make Bitwise
    // XOR of odd and even indexed elements
    // equal by removing an array element
    function Remove_one_element(arr, n)
    {
        // Stores xor of odd and even
        // indexed elements from the end
        let post_odd = 0, post_even = 0;
      
        // Stores xor of odd and even
        // indexed elements from the start
        let curr_odd = 0, curr_even = 0;
      
        // Stores the required count
        let res = 0;
      
        // Traverse the array in reverse
        for (let 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 (let 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
            let X = curr_odd ^ post_even;
      
            // Removing arr[i], post_odd stores
            // XOR of even indexed elements
            let 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
        document.write(res);
    }
  
 
// Driver Code
 
    // Given array
        let arr = [ 1, 0, 1, 0, 1 ];
      
        // Given size
        let N = arr.length;
      
        // Function call
        Remove_one_element(arr, N);
     
</script>
Output: 
3

 

Time Complexity: O(N)
Auxiliary Space: O(N)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!