📌  相关文章
📜  使二进制数组 K 周期性的最小移动次数

📅  最后修改于: 2022-05-13 01:57:52.622000             🧑  作者: Mango

使二进制数组 K 周期性的最小移动次数

给定一个二进制数组arr[] (仅包含 0 和 1)和一个整数K。任务是找到使数组 K 周期的最小移动次数。
如果子数组[1 到 K][k+1 到 2K][2k+1 到 3K] …… 都完全相同,则称一个数组是 K 周期的。
在一次移动中,任何 1 都可以变为 0,或者任何 0 都可以变为 1。

例子:

方法:对于一个数组是 K 周期的。考虑索引i其中i % K = X 。所有这些索引必须具有相同的值。因此,1 可以转换为 0,反之亦然。为了减少移动次数,我们选择最小的转换。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to return the minimum
// moves required
static int minMoves(int n, int a[], int k)
{
    int ct1[] = new int [k];
    int ct0[] = new int[k];
    int 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 += Math.min(ct1[i], ct0[i]);
 
    // Return the minimum moves required
    return moves;
}
 
// Driver code
public static void main (String[] args)
{
    int k = 2;
    int a[] = { 1, 0, 0, 0, 1, 0 };
    int n = a.length;
    System.out.println(minMoves(n, a, k));
}
}
 
// This is code contributed by inder_verma


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# moves required
def minMoves(n, a, k):
    ct1 = [0 for i in range(k)]
    ct0 = [0 for i in range(k)]
    moves = 0
 
    # Count the number of 1s and 2s
    # at each X such that i % K = X
    for i in range(n):
        if (a[i] == 1):
            ct1[i % k] += 1
        else:
            ct0[i % k] += 1
 
    # Choose the minimum elements to change
    for i in range(k):
        moves += min(ct1[i], ct0[i])
 
    # Return the minimum moves required
    return moves
 
# Driver code
if __name__ == '__main__':
    k = 2
    a = [1, 0, 0, 0, 1, 0]
    n = len(a)
    print(minMoves(n, a, k))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the minimum
// moves required
static int minMoves(int n, int[] a, int k)
{
    int[] ct1 = new int [k];
    int[] ct0 = new int[k];
    int 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 += Math.Min(ct1[i], ct0[i]);
 
    // Return the minimum moves required
    return moves;
}
 
// Driver code
public static void Main ()
{
    int k = 2;
    int[] a = { 1, 0, 0, 0, 1, 0 };
    int n = a.Length;
    Console.WriteLine(minMoves(n, a, k));
}
}
 
// This is code contributed by Code_Mech


PHP


Javascript


输出:
1

时间复杂度: O(N)