给定长度为N的数组arr [] ,任务是查找数组索引的数量,以便从这些索引中删除元素使奇数索引元素和偶数索引(基于1的索引)元素的按位异或相等。
例子:
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.
天真的方法:解决此问题的最简单方法是遍历数组,对于每个数组元素,检查从数组中删除元素是否使偶数索引和奇数索引数组元素的按位XOR相等或不相等。如果发现为真,则增加计数。最后,打印计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:可以基于以下观察条件来优化上述方法:从给定数组中删除任何元素,使后续元素的偶数索引为奇数,而后续元素的奇数索引为偶数。请按照以下步骤解决问题:
- 用0初始化变量curr_odd,curr_even,post_odd,post_even和res 。
- 反向遍历数组并执行以下操作:
- 如果当前元素为奇数,则对post_odd进行XOR。
- 否则,将当前元素与post_even进行XOR。
- 现在,遍历给定的数组并执行以下操作:
- 如果当前索引为奇数,则通过使用post_odd和当前元素的按位XOR更新post_odd来从post_odd中删除当前元素。
- 否则,请类似地从post_even中删除当前元素。
- 初始化变量X和Y。
- 在X中分配curr_odd和post_even的XOR。因此, X存储所有奇数索引元素的异或。
- 在Y中指定curr_even和post_odd的xor。因此, Y存储所有偶数索引元素的异或。
- 检查X是否等于Y。如果确定为true,则将res增加1 。
- 如果当前索引为奇数,则将当前元素与curr_odd进行XOR。
- 否则,将当前元素与curr_even进行XOR。
- 最后,打印res 。
下面是上述方法的实现:
C++
// 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
// 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
输出:
3
时间复杂度: O(N)
辅助空间: O(N)