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++
#include <iostream>
#include <limits.h>
using namespace std;
int count_flips( int a[], int n, int k)
{
bool set = false ;
int ans = 0, min_diff = INT_MAX;
for ( int i = 0; i < k; i++) {
int curr_index = i, segment = 0,
count_zero = 0, count_one = 0;
while (curr_index < n) {
if (segment % 2 == 0) {
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
}
else {
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;
min_diff = min(min_diff,
abs (count_zero - count_one));
}
if (set)
return ans;
else
return ans + min_diff;
}
int main()
{
int n = 6, k = 3;
int a[] = { 0, 0, 1, 1, 0, 0 };
cout << count_flips(a, n, k);
}
|
Java
import java.util.*;
class Count_Flips {
public static int count_flips(
int a[], int n, int k){
boolean set = false ;
int ans = 0 ,
min_diff = Integer.MAX_VALUE;
for ( int i = 0 ; i < k; i++) {
int curr_index = i, segment = 0 ,
count_zero = 0 , count_one = 0 ;
while (curr_index < n) {
if (segment % 2 == 0 ) {
if (a[curr_index] == 1 )
count_zero++;
else
count_one++;
}
else {
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;
min_diff = Math.min(min_diff,
Math.abs(count_zero - count_one));
}
if (set)
return ans;
else
return ans + min_diff;
}
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
def count_flips(a, n, k):
min_diff, ans, set = n, 0 , False
for i in range (k):
curr_index, segment,\
count_zero, count_one = \
i, 0 , 0 , 0
while curr_index < n:
if segment % 2 = = 0 :
if a[curr_index] = = 1 :
count_zero + = 1
else :
count_one + = 1
else :
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))
if set :
return ans
else :
return ans + min_diff
if __name__ = = "__main__" :
n, k = 6 , 3
a = [ 0 , 0 , 1 , 1 , 0 , 0 ]
print (count_flips(a, n, k))
|
C#
using System;
class Count_Flips
{
static int count_flips( int []a, int n, int k)
{
bool set = false ;
int ans = 0, min_diff = int .MaxValue;
for ( int i = 0; i < k; i++) {
int curr_index = i, segment = 0,
count_zero = 0, count_one = 0;
while (curr_index < n) {
if (segment % 2 == 0) {
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
}
else {
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 ;
min_diff = Math.Min(min_diff,
Math.Abs(count_zero - count_one));
}
if ( set )
return ans;
else
return ans + min_diff;
}
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));
}
}
|
Javascript
<script>
function count_flips(a , n , k) {
var set = false ;
var ans = 0, min_diff = Number.MAX_VALUE;
for (i = 0; i < k; i++) {
var curr_index = i, segment = 0, count_zero = 0, count_one = 0;
while (curr_index < n) {
if (segment % 2 == 0) {
if (a[curr_index] == 1)
count_zero++;
else
count_one++;
} else {
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;
min_diff = Math.min(min_diff, Math.abs(count_zero - count_one));
}
if (set)
return ans;
else
return ans + min_diff;
}
var n = 6, k = 3;
var a = [ 0, 0, 1, 1, 0, 0 ];
document.write(count_flips(a, n, k));
</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).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...