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:
- Removing an element from index 3 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
- Removing an element from index 1 modifies arr[] to {0, 1, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
- 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: 3
- Removing an element from index 3 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 0.
- Removing an element from index 2 modifies arr[] to {1, 0, 0, 1}. Therefore, xor of odd and even indexed elements is 1.
- 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:
- Initialize variables curr_odd, curr_even, post_odd, post_even, and res with 0.
- 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.
- 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.
- Finally, print the res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Remove_one_element( int arr[], int n)
{
int post_odd = 0, post_even = 0;
int curr_odd = 0, curr_even = 0;
int res = 0;
for ( int i = n - 1; i >= 0; i--) {
if (i % 2)
post_odd ^= arr[i];
else
post_even ^= arr[i];
}
for ( int i = 0; i < n; i++) {
if (i % 2)
post_odd ^= arr[i];
else
post_even ^= arr[i];
int X = curr_odd ^ post_even;
int Y = curr_even ^ post_odd;
if (X == Y)
res++;
if (i % 2)
curr_odd ^= arr[i];
else
curr_even ^= arr[i];
}
cout << res << endl;
}
int main()
{
int arr[] = { 1, 0, 1, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
Remove_one_element(arr, N);
return 0;
}
|
Java
class GFG {
static void Remove_one_element( int arr[], int n)
{
int post_odd = 0 , post_even = 0 ;
int curr_odd = 0 , curr_even = 0 ;
int res = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (i % 2 != 0 )
post_odd ^= arr[i];
else
post_even ^= arr[i];
}
for ( int i = 0 ; i < n; i++)
{
if (i % 2 != 0 )
post_odd ^= arr[i];
else
post_even ^= arr[i];
int X = curr_odd ^ post_even;
int Y = curr_even ^ post_odd;
if (X == Y)
res++;
if (i % 2 != 0 )
curr_odd ^= arr[i];
else
curr_even ^= arr[i];
}
System.out.println(res);
}
public static void main (String[] args)
{
int arr[] = { 1 , 0 , 1 , 0 , 1 };
int N = arr.length;
Remove_one_element(arr, N);
}
}
|
Python3
def Remove_one_element(arr, n):
post_odd = 0
post_even = 0
curr_odd = 0
curr_even = 0
res = 0
for i in range (n - 1 , - 1 , - 1 ):
if (i % 2 ):
post_odd ^ = arr[i]
else :
post_even ^ = arr[i]
for i in range (n):
if (i % 2 ):
post_odd ^ = arr[i]
else :
post_even ^ = arr[i]
X = curr_odd ^ post_even
Y = curr_even ^ post_odd
if (X = = Y):
res + = 1
if (i % 2 ):
curr_odd ^ = arr[i]
else :
curr_even ^ = arr[i]
print (res)
if __name__ = = "__main__" :
arr = [ 1 , 0 , 1 , 0 , 1 ]
N = len (arr)
Remove_one_element(arr, N)
|
C#
using System;
class GFG {
static void Remove_one_element( int [] arr, int n)
{
int post_odd = 0, post_even = 0;
int curr_odd = 0, curr_even = 0;
int res = 0;
for ( int i = n - 1; i >= 0; i--)
{
if (i % 2 != 0)
post_odd ^= arr[i];
else
post_even ^= arr[i];
}
for ( int i = 0; i < n; i++)
{
if (i % 2 != 0)
post_odd ^= arr[i];
else
post_even ^= arr[i];
int X = curr_odd ^ post_even;
int Y = curr_even ^ post_odd;
if (X == Y)
res++;
if (i % 2 != 0)
curr_odd ^= arr[i];
else
curr_even ^= arr[i];
}
Console.WriteLine(res);
}
public static void Main ()
{
int [] arr = { 1, 0, 1, 0, 1 };
int N = arr.Length;
Remove_one_element(arr, N);
}
}
|
Javascript
<script>
function Remove_one_element(arr, n)
{
let post_odd = 0, post_even = 0;
let curr_odd = 0, curr_even = 0;
let res = 0;
for (let i = n - 1; i >= 0; i--)
{
if (i % 2 != 0)
post_odd ^= arr[i];
else
post_even ^= arr[i];
}
for (let i = 0; i < n; i++)
{
if (i % 2 != 0)
post_odd ^= arr[i];
else
post_even ^= arr[i];
let X = curr_odd ^ post_even;
let Y = curr_even ^ post_odd;
if (X == Y)
res++;
if (i % 2 != 0)
curr_odd ^= arr[i];
else
curr_even ^= arr[i];
}
document.write(res);
}
let arr = [ 1, 0, 1, 0, 1 ];
let N = arr.length;
Remove_one_element(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) because constant space is being used.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Oct, 2022
Like Article
Save Article