Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
Last Updated :
10 Sep, 2021
Given a binary array arr[] of length N, the task is to find the minimum flips required in the array such that XOR of a consecutive sub-arrays of size K have different parity.
Examples:
Input: arr[] = {0, 1, 0, 1, 1}, K = 2
Output: 2
Explanation:
For the above given array XOR of consecutive 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 consecutive 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.
Approach: To make the different parity of consecutive subarrays, the total array is dependent upon the first subarray of size K. That is every next subarray of size K should be the negation of the previous subarray.
For Example: For an array of size 4, such that consecutive subarray of size 2 have different parity:
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}
Below is the implementation of the above approach:
C++
// C++ implementation to find the
// minimum flips required such that
// alternate subarrays have
// different parity
#include <iostream>
#include <limits.h>
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<count_zero:
set = not set
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
if __name__ == "__main__":
n, k = 6, 3
a =[0, 0, 1, 1, 0, 0]
print(count_flips(a, n, k))
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
<script>
// Javascript implementation to find the minimum flips
// required such that alternate subarrays
// have different parity
// Function to find the minimum
// flips required in binary array
function count_flips(a , n , k) {
// Boolean value to indicate
// odd or even value of 1's
var set = false;
var ans = 0, min_diff = Number.MAX_VALUE;
// Loop to iterate over
// the subarrays of size K
for (i = 0; i < k; i++) {
// curr_index is used to iterate
// over all the subarrays
var 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
var n = 6, k = 3;
var a = [ 0, 0, 1, 1, 0, 0 ];
document.write(count_flips(a, n, k));
// This code contributed by Rajput-Ji
</script>
Performance Analysis:
- Time Complexity: As in the above approach, There is only one loop which takes O(N) time in worst case. Hence the Time Complexity will be O(N).
- Auxiliary Space Complexity: As in the above approach, There is no extra space used. Hence the auxiliary space complexity will be O(1).
Similar Reads
Minimum Subarray flips required to convert all elements of a Binary Array to K The problem statement is asking for the minimum number of operations required to convert all the elements of a given binary array arr[] to a specified value K, where K can be either 0 or 1. The operations can be performed on any index X of the array, and the operation is to flip all the elements of
8 min read
Minimum count of increment of K size subarrays required to form a given Array Given an array arr[] and an integer K, the task is to find the minimum number of operations required to change an array B of size N containing all zeros such that every element of B is greater than or equal to arr. i.e., arr[i] >= B[i]. In any operation, you can choose a subarray of B of size K a
8 min read
Minimum flips required in a binary string such that all K-size substring contains 1 Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one '1'.Examples: Input: str = "0001", K = 2 Output: 1 Explanation: Flipping the bit at index 1 modifies str to "0101". All substrin
11 min read
Split array into K Subarrays to minimize sum of difference between min and max Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e
6 min read
Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K Given two binary matrices A[] and B[] of dimension N * M and a positive integer K, the task is to find the minimum number of flipping of submatrix of size K required in the matrix A[][] to convert it into the matrix B[][]. If it is not possible to convert then print "-1". Examples: Input: A[][] = {
8 min read
Create an array such that XOR of subarrays of length K is X Given three integers N, K and X, the task is to construct an array of length N, in which XOR of all elements of each contiguous sub-array of length K is X.Examples: Input: N = 5, K = 1, X = 4 Output: 4 4 4 4 4 Explanation: Each subarray of length 1 has Xor value equal to 4.Input: N = 5, K = 2, X = 4
4 min read
Count of subarrays of size K having at least one pair with absolute difference divisible by K-1 Given an arr[] consisting of N elements, the task is to count all subarrays of size K having atleast one pair whose absolute difference is divisible by K - 1.Examples: Input: arr[] = {1, 5, 3, 2, 17, 18}, K = 4 Output: 3 Explanation: The three subarrays of size 4 are: {1, 5, 3, 2}: Pair {5, 2} have
4 min read
Split array into K subarrays with minimum sum of absolute difference between adjacent elements Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray. Examples: Input: arr[] = {1, 3, -2, 5, -1}, K = 2Output: 13Explanation: Split the array into following 2 subarrays:
8 min read
Find a K-length subarray having Bitwise XOR equal to that of remaining array elements Given an array arr[] of size N, the task is to check if any subarray of size K exists in the array or not, whose Bitwise XOR is equal to the Bitwise XOR of the remaining array elements. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 },
9 min read
Construct a sorted Array such that setbit in bitwise XOR of any pair is even Given an integer N(1 ⤠N ⤠500), Construct an integer arr[] of length N, such that it should follow all the given conditions below: Each element of arr[] should be distinct.Binary representation of (arr[i]^arr[j]) should contain even number of set bits for all ( 1â¤iâ¤N ) and ( iâ j ).All the elements
7 min read