# Minimum number of moves to make a binary array K periodic

Given a binary array **arr[]** (containing only 0s and 1s) and an integer **K**. The task is to find the minimum number of moves to make the array K-periodic.

An array is said to be K-periodic if the sub-arrays **[1 to K]**, **[k+1 to 2K]**, **[2k+1 to 3K], …** are all exactly same.

In a single move any 1 can be changed to a 0 or any 0 can be changed into a 1.

**Examples:**

Input:arr[] = {1, 1, 0, 0, 1, 1}, K = 2

Output:2

The new array can be {1, 1, 1, 1, 1, 1}

Input:arr[] = {1, 0, 0, 0, 1, 0}, K = 2

Output:1

The new array can be {1, 0, 1, 0, 1, 0}

**Approach:** For an array to be K-periodic. Consider indices **i** where **i % K = X**. All these indices must have the same value. So either the 1s can be converted to 0s or vice-versa. In order to reduce the number of moves, we choose the conversion which is minimum.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum moves required ` `int` `minMoves(` `int` `n, ` `int` `a[], ` `int` `k) ` `{ ` ` ` ` ` `int` `ct1[k] = { 0 }, ct0[k] = { 0 }, moves = 0; ` ` ` ` ` `// Count the number of 1s and 2s ` ` ` `// at each X such that i % K = X ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(a[i] == 1) ` ` ` `ct1[i % k]++; ` ` ` `else` ` ` `ct0[i % k]++; ` ` ` ` ` `// Choose the minimum elements to change ` ` ` `for` `(` `int` `i = 0; i < k; i++) ` ` ` `moves += min(ct1[i], ct0[i]); ` ` ` ` ` `// Return the minimum moves required ` ` ` `return` `moves; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 2; ` ` ` `int` `a[] = { 1, 0, 0, 0, 1, 0 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << minMoves(n, a, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(N)

## Recommended Posts:

- Minimum number of moves to make all elements equal
- Count minimum number of "move-to-front" moves to sort an array
- Minimum number of changes required to make the given array an AP
- Minimum number of operations on an array to make all elements 0
- Minimum number of subtract operation to make an array decreasing
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Find minimum number of merge operations to make an array palindrome
- Minimum number of increment-other operations to make all array elements equal.
- Minimum number of 1's to be replaced in a binary array
- Minimum swaps required to make a binary string alternating
- Minimum removals from array to make max - min <= K
- Minimum removals to make array sum even
- Minimum removals to make array sum odd
- Minimum operations to make XOR of array zero
- Minimum gcd operations to make all array elements one

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.