给定一个长度为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.
朴素的方法:解决这个问题的最简单的方法是遍历数组,对于每个数组元素,检查从数组中删除元素是否使偶数索引和奇数索引数组元素的按位异或相等。如果发现为真,则增加计数。最后,打印计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效方法:上述方法可以基于以下观察进行优化:从给定数组中删除任何元素会使后续元素的偶数索引为奇数,而后继元素的奇数索引为偶数。请按照以下步骤解决问题:
- 初始化变量curr_odd,curr_even,post_odd,post_even和RES 0。
- 反向遍历数组并执行以下操作:
- 如果当前元素是奇数,则与post_odd异或。
- 否则,将当前元素与post_even 进行异或。
- 现在,遍历给定的数组并执行以下操作:
- 如果当前索引为奇数,则通过使用post_odd和当前元素的按位异或更新post_odd 来从post_odd 中删除当前元素。
- 否则,类似地从post_even 中删除当前元素。
- 初始化变量X和Y 。
- 在X 中分配curr_odd和post_even 的异或。因此, X存储所有奇数索引元素的异或。
- 在Y 中分配curr_even和post_odd 的异或。因此, Y存储所有偶数索引元素的异或。
- 检查X是否等于Y 。如果发现为真,则将res增加1 。
- 如果当前索引为奇数,则将当前元素与curr_odd进行异或。
- 否则,将当前元素与curr_even 进行异或。
- 最后,打印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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。