给定一个0和1的二进制数组,任务是找到从该数组中精确擦除一个元素以使XOR为零的方法。
例子:
Input: arr = {1, 1, 1, 1, 1 }
Output: 5
You can erase any of the given 5 1's,
it will make the XOR of the rest equal to zero.
Input: arr = {1, 0, 0, 1, 0 }
Output: 3
Since the XOR of array is already 0,
You can erase any of the given 3 0's
so that the XOR remains unaffected.
方法:既然我们知道,要使二进制元素的XOR为0,则1的数量应为偶数。因此,此问题可以分为4种情况:
- 在给定的数组中,当1的数量为偶数而0的数量也为偶数时:在这种情况下,该数组的XOR已经为0。因此,为了不影响XOR,我们只能删除0。因此,从阵列中精确擦除一个元素以使XOR为零的方法的数量就是该阵列中0的数量。
- 在给定数组中,当1的数量为偶数而0的数量为奇数时:在这种情况下,该数组的XOR已经为0。因此,为了不影响XOR,我们只能删除0。因此,从阵列中精确擦除一个元素以使XOR为零的方法的数量就是该阵列中0的数量。
- 在给定数组中,当1的数量为奇数而0的数量为偶数时:在这种情况下,该数组的XOR为1。因此,要使XOR为0,我们可以删除1中的任何一个。因此,从阵列中精确擦除一个元素以使XOR为零的方法的数量就是该阵列中1的数量。
- 在给定数组中,当1的数量为奇数,而0的数量也为奇数时:在这种情况下,该数组的XOR为1。因此,要使XOR为0,我们可以删除任何1。因此,从该数组中精确擦除一个元素以使XOR为零的方法的数量就是该数组中1的数量。
下面是上述方法的实现:
C++
// C++ program to find the number of ways
// to erase exactly one element
// from this array to make XOR zero
#include
using namespace std;
// Function to find the number of ways
int no_of_ways(int a[], int n)
{
int count_0 = 0, count_1 = 0;
// Calculate the number of 1's and 0's
for (int i = 0; i < n; i++) {
if (a[i] == 0)
count_0++;
else
count_1++;
}
// Considering the 4 cases
if (count_1 % 2 == 0)
return count_0;
else
return count_1;
}
// Driver code
int main()
{
int n = 4;
int a1[4] = { 1, 1, 0, 0 };
cout << no_of_ways(a1, n) << endl;
n = 5;
int a2[5] = { 1, 1, 1, 0, 0 };
cout << no_of_ways(a2, n) << endl;
n = 5;
int a3[5] = { 1, 1, 0, 0, 0 };
cout << no_of_ways(a3, n) << endl;
n = 6;
int a4[6] = { 1, 1, 1, 0, 0, 0 };
cout << no_of_ways(a4, n) << endl;
return 0;
}
Java
// Java program to find the number of ways
// to erase exactly one element
// from this array to make XOR zero
class GFG
{
// Function to find the number of ways
static int no_of_ways(int a[], int n)
{
int count_0 = 0, count_1 = 0;
// Calculate the number of 1's and 0's
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
count_0++;
else
count_1++;
}
// Considering the 4 cases
if (count_1 % 2 == 0)
return count_0;
else
return count_1;
}
// Driver code
public static void main (String[] args)
{
int n = 4;
int a1[] = { 1, 1, 0, 0 };
System.out.println(no_of_ways(a1, n));
n = 5;
int a2[] = { 1, 1, 1, 0, 0 };
System.out.println(no_of_ways(a2, n));
n = 5;
int a3[] = { 1, 1, 0, 0, 0 };
System.out.println(no_of_ways(a3, n));
n = 6;
int a4[] = { 1, 1, 1, 0, 0, 0 };
System.out.println(no_of_ways(a4, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find the number of ways
# to erase exactly one element
# from this array to make XOR zero
# Function to find the number of ways
def no_of_ways(a, n):
count_0 = 0
count_1 = 0
# Calculate the number of 1's and 0's
for i in range(0, n):
if (a[i] == 0):
count_0 += 1
else:
count_1 += 1
# Considering the 4 cases
if (count_1 % 2 == 0):
return count_0
else:
return count_1
# Driver code
if __name__ == '__main__':
n = 4
a1 = [ 1, 1, 0, 0 ]
print(no_of_ways(a1, n))
n = 5
a2 = [ 1, 1, 1, 0, 0 ]
print(no_of_ways(a2, n))
n = 5
a3 = [ 1, 1, 0, 0, 0 ]
print(no_of_ways(a3, n))
n = 6
a4 = [ 1, 1, 1, 0, 0, 0 ]
print(no_of_ways(a4, n))
# This code is contributed by ashutosh450
C#
// C# program to find the number of ways
// to erase exactly one element
// from this array to make XOR zero
using System;
class GFG
{
// Function to find the number of ways
static int no_of_ways(int []a, int n)
{
int count_0 = 0, count_1 = 0;
// Calculate the number of 1's and 0's
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
count_0++;
else
count_1++;
}
// Considering the 4 cases
if (count_1 % 2 == 0)
return count_0;
else
return count_1;
}
// Driver code
public static void Main ()
{
int n = 4;
int [] a1 = { 1, 1, 0, 0 };
Console.WriteLine(no_of_ways(a1, n));
n = 5;
int [] a2 = { 1, 1, 1, 0, 0 };
Console.WriteLine(no_of_ways(a2, n));
n = 5;
int [] a3 = { 1, 1, 0, 0, 0 };
Console.WriteLine(no_of_ways(a3, n));
n = 6;
int [] a4 = { 1, 1, 1, 0, 0, 0 };
Console.WriteLine(no_of_ways(a4, n));
}
}
// This code is contributed by Mohit kumar
输出:
2
3
3
3
时间复杂度: O(n)