两个二进制数组中的最小翻转,使得它们的 XOR 等于另一个数组
给定三个二进制数组,每个数组的大小为n ,任务是在第一个和第二个数组中找到最小位翻转,使得第一个和第二个数组的第 i 个索引位的 XOR 等于第三个数组的第 i 个索引位大批。给定一个约束,我们最多只能翻转数组 1 的 p 位和数组 2 的最多 q 位。如果不可能,则输出 -1。
不允许重新排列位。
例子 :
Input : p = 2, q = 2
arr1[] = {0, 0, 1}
arr2[] = {0, 1, 0}
arr3[] = {0, 1, 0}
Output : 1
arr1[0] ^ arr2[0] = 0 ^ 0 = 0, which is equal
to arr3[0], so no flip required.
arr1[1] ^ arr2[1] = 0 ^ 1 = 1, which is equal
to arr3[1], so no flip required.
arr1[2] ^ arr2[2] = 1 ^ 0 = 1, which is not
equal to arr3[0], so one flip required.
Also p = 2 and q = 2, so flip arr1[2].
Input : p = 2, q = 4
arr1 = { 1, 0, 1, 1, 1, 1, 1 }
arr2 = { 0, 1, 1, 1, 1, 0, 0 }
arr3 = { 1, 1, 1, 1, 0, 0, 1 }
Output : 3
When the XOR of i'th bit of array1 and arry2 is
equal to i'th bit of array3, no flip is required.
Now let's observe when XOR is not equal.
There can be following cases:
Case 1: When arr3[i] = 0,
then either arr1[i] = 1, arr2[i] = 0 or
arr1[i] = 0, arr2[i] = 1.
Case 2: When arr3[i] = 1,
then either arr1[i] = 1, arr2[i] = 1 or
arr1[i] = 0, arr2[i] = 0.
At least one flip is required in each case.
对于情况 1,XOR 应为 0,可以通过 0 ^ 0 或 1 ^ 1 获得;对于情况 2,可以通过 1 ^ 0 或 0 ^ 1 获得 1。
因此,请注意我们可以根据 p 和 q 的值翻转 arr1[i] 或 arr2[i]。
如果 p = 0,则需要翻转 arr2,如果 q 也为 0,则输出 -1。同样,如果 p = 0,则需要翻转 arr1,如果 p 也为 0,则输出 -1
因此,我们可以说使 arr1 和 arr2 的 XOR 等于 arr3 所需的翻转次数应该小于或等于 p + q。
下面是这种方法的实现:
C++
// C++ program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
#include
using namespace std;
// Return minimum number of flip required
int minflip(int arr1[], int arr2[], int arr3[],
int p, int q, int n)
{
int flip = 0;
// Counting number of mismatch, XOR of arr1[] and
// arr2[] is not equal to arr3[].
for (int i = 0; i < n; i++)
if (arr1[i] ^ arr2[i] != arr3[i])
flip++;
// if flip is less then allowed constraint return
// it. else return -1.
return (flip <= p + q) ? flip : -1;
}
// Driven Program
int main()
{
int arr1[] = { 1, 0, 1, 1, 1, 1, 1 };
int arr2[] = { 0, 1, 1, 1, 1, 0, 0 };
int arr3[] = { 1, 1, 1, 1, 0, 0, 1 };
int n = sizeof(arr1) / sizeof(arr1[0]);
int p = 2, q = 4;
cout << minflip(arr1, arr2, arr3, p, q, n);
return 0;
}
Java
// Java program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
import java.io.*;
class GFG {
// Return minimum number of flip required
static int minflip(int[] arr1, int[] arr2, int[] arr3,
int p, int q, int n)
{
int flip = 0;
// Counting number of mismatch, XOR of arr1[] and
// arr2[] is not equal to arr3[].
for (int i = 0; i < n; i++)
if (arr1[i] > 0 ^ arr2[i] > 0 != arr3[i] > 0)
flip++;
// if flip is less then allowed constraint return
// it. else return -1.
return (flip <= p + q) ? flip : -1;
}
// Driver program
static public void main(String[] args)
{
int[] arr1 = {1, 0, 1, 1, 1, 1, 1};
int[] arr2 = {0, 1, 1, 1, 1, 0, 0};
int[] arr3 = {1, 1, 1, 1, 0, 0, 1};
int n = arr1.length;
int p = 2, q = 4;
System.out.println(minflip(arr1, arr2, arr3, p, q, n));
}
}
// This code is contributed by vt_m.
Python3
# Python 3 program to find
# minimum flip required to
# make XOR of two arrays
# equal to another array
# with constraints on number
# of flip on each array.
# Return minimum number
# of flip required
def minflip(arr1, arr2,
arr3, p, q, n):
flip = 0
# Counting number of
# mismatch, XOR of
# arr1[] and arr2[]
# is not equal to arr3[].
for i in range(0 , n):
if (arr1[i] ^
arr2[i] != arr3[i]):
flip += 1
# if flip is less then
# allowed constraint return
# it. else return -1.
return flip if (flip <= p + q) else -1
# Driver Code
arr1 = [1, 0, 1, 1, 1, 1, 1]
arr2 = [0, 1, 1, 1, 1, 0, 0]
arr3 = [1, 1, 1, 1, 0, 0, 1]
n = len(arr1)
p = 2
q = 4
print(minflip(arr1, arr2,
arr3, p, q, n))
# This code is contributed
# by Smitha
C#
// C# program to find minimum flip required to make
// XOR of two arrays equal to another array with
// constraints on number of flip on each array.
using System;
class GFG {
// Return minimum number of flip required
static int minflip(int[] arr1, int[] arr2, int[] arr3,
int p, int q, int n)
{
int flip = 0;
// Counting number of mismatch, XOR of arr1[] and
// arr2[] is not equal to arr3[].
for (int i = 0; i < n; i++)
if (arr1[i] > 0 ^ arr2[i] > 0 != arr3[i] > 0)
flip++;
// if flip is less then allowed constraint return
// it. else return -1.
return (flip <= p + q) ? flip : -1;
}
// Driver program
static public void Main()
{
int[] arr1 = { 1, 0, 1, 1, 1, 1, 1 };
int[] arr2 = { 0, 1, 1, 1, 1, 0, 0 };
int[] arr3 = { 1, 1, 1, 1, 0, 0, 1 };
int n = arr1.Length;
int p = 2, q = 4;
Console.WriteLine(minflip(arr1, arr2, arr3, p, q, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
3