给定一个长度为N的二进制数组arr[] ,任务是找到数组中所需的最小翻转次数,使得大小为 K 的连续子数组的 XOR 具有不同的奇偶校验。
例子:
Input: arr[] = {0, 1, 0, 1, 1}, K = 2
Output: 2
Explanation:
For the above given array XOR of consective sub-array of size 2 is: {(0, 1): 1, (1, 0): 1, (0, 1): 1, (1, 1): 0}
There are two flips required which can be done on the following indices:
Index 0: It is required to flip the bit of the 0th index, such that XOR of first sub-array remains 1.
Index 1: It is required to flip the bit of 1st index, such that XOR of second sub-array becomes 0.
Input: arr[]={0, 0, 1, 1, 0, 0}, K = 3
Output: 1
Explanation:
For the above given array XOR of consective sub-array of size 2 is: {(0, 0, 1): 1, (0, 1, 1): 0, (1, 1, 0): 0, (1, 0, 0): 1}
There is one flip required which can be done on the following indices:
Index 4: It is required to flip the bit of the 4th index, such that XOR of third sub-array becomes 1 and XOR of fourth subarray becomes 0.
方法:为了使连续子数组的奇偶性不同,整个数组依赖于第一个大小为K的子数组。也就是说,每个下一个大小为K的子数组都应该是前一个子数组的否定。
例如:对于大小为 4 的数组,使得大小为 2 的连续子数组具有不同的奇偶校验:
Let the first subarray of size 2 be {1, 1}
Then the next subarray can be {0, 0}
Consecutive subarrays of size 2 in this array:
{(1, 1): 0, (1, 0): 1, (0, 0): 0}
下面是上述方法的实现:
C++
// C++ implementation to find the
// minimum flips required such that
// alternate subarrays have
// different parity
#include
#include
using namespace std;
// Function to find the minimum
// flips required in binary array
int count_flips(int a[], int n, int k)
{
// Boolean value to indicate
// odd or even value of 1's
bool set = false;
int ans = 0, min_diff = INT_MAX;
// Loop to iterate over
// the subarrays of size K
for (int i = 0; i < k; i++) {
// curr_index is used to iterate
// over all the subarrays
int curr_index = i, segment = 0,
count_zero = 0, count_one = 0;
// Loop to iterate over the array
// at the jump of K to
// consider every parity
while (curr_index < n) {
// Condition to check if the
// subarray is at even position
if (segment % 2 == 0) {
// The value needs to be
// same as the first subarray
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
}
else {
// The value needs to be
// opposite of the first subarray
if (a[curr_index] == 0)
count_zero++;
else
count_one++;
}
curr_index = curr_index + k;
segment++;
}
ans += min(count_one, count_zero);
if (count_one < count_zero)
set = !set;
// Update the minimum difference
min_diff = min(min_diff,
abs(count_zero - count_one));
}
// Condition to check if the 1s
// in the subarray is odd
if (set)
return ans;
else
return ans + min_diff;
}
// Driver Code
int main()
{
int n = 6, k = 3;
int a[] = { 0, 0, 1, 1, 0, 0 };
cout << count_flips(a, n, k);
}
Java
// Java implementation to find the minimum flips
// required such that alternate subarrays
// have different parity
import java.util.*;
class Count_Flips {
// Function to find the minimum
// flips required in binary array
public static int count_flips(
int a[], int n, int k){
// Boolean value to indicate
// odd or even value of 1's
boolean set = false;
int ans = 0,
min_diff = Integer.MAX_VALUE;
// Loop to iterate over
// the subarrays of size K
for (int i = 0; i < k; i++) {
// curr_index is used to iterate
// over all the subarrays
int curr_index = i, segment = 0,
count_zero = 0, count_one = 0;
// Loop to iterate over the array
// at the jump of K to
// consider every parity
while (curr_index < n) {
// Condition to check if the
// subarray is at even position
if (segment % 2 == 0) {
// The value needs to be
// same as the first subarray
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
}
else {
// The value needs to be
// opposite of the first subarray
if (a[curr_index] == 0)
count_zero++;
else
count_one++;
}
curr_index = curr_index + k;
segment++;
}
ans += Math.min(count_one, count_zero);
if (count_one < count_zero)
set = !set;
// Update the minimum difference
min_diff = Math.min(min_diff,
Math.abs(count_zero - count_one));
}
// Condition to check if the 1s
// in the subarray is odd
if (set)
return ans;
else
return ans + min_diff;
}
// Driver Code
public static void main(String[] args)
{
int n = 6, k = 3;
int a[] = { 0, 0, 1, 1, 0, 0 };
System.out.println(count_flips(a, n, k));
}
}
Python3
# Python implementation to find the
# minimum flips required such that
# alternate subarrays have
# different parity
# Function to find the minimum
# flips required in binary array
def count_flips(a, n, k):
min_diff, ans, set = n, 0, False
# Loop to iterate over
# the subarrays of size K
for i in range(k):
# curr_index is used to iterate
# over all the subarrays
curr_index, segment,\
count_zero, count_one =\
i, 0, 0, 0
# Loop to iterate over the array
# at the jump of K to
# consider every parity
while curr_index < n:
# Condition to check if the
# subarray is at even position
if segment % 2 == 0:
# The value needs to be
# same as the first subarray
if a[curr_index] == 1:
count_zero += 1
else:
count_one += 1
else:
# The value needs to be
# opposite of the first subarray
if a[curr_index] == 0:
count_zero += 1
else:
count_one += 1
curr_index += k
segment+= 1
ans += min(count_zero, count_one)
if count_one
C#
// C# implementation to find the minimum flips
// required such that alternate subarrays
// have different parity
using System;
class Count_Flips
{
// Function to find the minimum
// flips required in binary array
static int count_flips(int []a, int n, int k)
{
// Boolean value to indicate
// odd or even value of 1's
bool set = false;
int ans = 0, min_diff = int.MaxValue;
// Loop to iterate over
// the subarrays of size K
for (int i = 0; i < k; i++) {
// curr_index is used to iterate
// over all the subarrays
int curr_index = i, segment = 0,
count_zero = 0, count_one = 0;
// Loop to iterate over the array
// at the jump of K to
// consider every parity
while (curr_index < n) {
// Condition to check if the
// subarray is at even position
if (segment % 2 == 0) {
// The value needs to be
// same as the first subarray
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
}
else {
// The value needs to be
// opposite of the first subarray
if (a[curr_index] == 0)
count_zero++;
else
count_one++;
}
curr_index = curr_index + k;
segment++;
}
ans += Math.Min(count_one, count_zero);
if (count_one < count_zero)
set = !set;
// Update the minimum difference
min_diff = Math.Min(min_diff,
Math.Abs(count_zero - count_one));
}
// Condition to check if the 1s
// in the subarray is odd
if (set)
return ans;
else
return ans + min_diff;
}
// Driver Code
public static void Main(string[] args)
{
int n = 6, k = 3;
int []a = { 0, 0, 1, 1, 0, 0 };
Console.WriteLine(count_flips(a, n, k));
}
}
// This code is contributed by Yash_R
Javascript
1
性能分析:
- 时间复杂度:与上述方法一样,只有一个循环在最坏情况下花费 O(N) 时间。因此时间复杂度将为O(N) 。
- 辅助空间复杂度:与上述方法一样,没有使用额外的空间。因此辅助空间复杂度将为O(1) 。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live