📌  相关文章
📜  将二进制数组的所有元素转换为 K 所需的最小子数组翻转

📅  最后修改于: 2021-10-26 05:17:43             🧑  作者: Mango

给定一个由N 个整数组成的二进制数组arr[] ,任务是计算将数组的所有元素转换为等于K所需的以下类型的最小操作数:

  • 从给定数组中选择任何索引X。
  • 翻转子数组 arr[X] … arr[N – 1] 的所有元素,即如果 arr[i] = 1,则将 arr[i] 设置为 0,反之亦然。

例子:

方法:应进行以下观察:

请按照以下步骤解决上述问题:

  1. 将变量标志初始化为K 的倒数。即如果 K = 0 则为 1,反之亦然,表示当前值。
  2. 将变量cnt初始化为 0,以保持数组 arr[] 中变化点数的计数。
  3. 遍历数组ARR []以及对于每个索引i应用以下步骤:
    • 如果flag值和arr[i]值不同,则进入下一次迭代。
    • 如果flagarr[i]相等,则增加count并将flag设置为flag = (flag + 1) % 2
  4. 打印count的最终值。

下面是上述方法的实现:

C++14
// C++14 Program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of subarray flips required
int minSteps(int arr[], int n, int k)
{
    int i, cnt = 0;
    int flag;
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for (i = 0; i < n; i++) {
 
        // If arr[i] and flag are equal
        if (arr[i] == flag) {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = sizeof(arr)
            / sizeof(arr[0]);
    int k = 1;
 
    cout << minSteps(arr, n, k);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to count the minimum
// number of subarray flips required
static int minSteps(int arr[], int n, int k)
{
    int i, cnt = 0;
    int flag;
     
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for(i = 0; i < n; i++)
    {
 
        // If arr[i] and flag are equal
        if (arr[i] == flag)
        {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = arr.length;
    int k = 1;
     
    System.out.print(minSteps(arr, n, k));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
# Function to count the minimum
# number of subarray flips required
def minSteps(arr, n, k):
 
    cnt = 0
    if(k == 1):
        flag = 0
    else:
        flag = 1
 
    # Iterate the array
    for i in range(n):
 
        # If arr[i] and flag are equal
        if(arr[i] == flag):
            cnt += 1
            flag = (flag + 1) % 2
 
    # Return the answer
    return cnt
 
# Driver Code
arr = [ 1, 0, 1, 0, 0, 1, 1, 1 ]
n = len(arr)
k = 1
 
# Function call
print(minSteps(arr, n, k))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to count the minimum
// number of subarray flips required
static int minSteps(int[] arr, int n, int k)
{
    int i, cnt = 0;
    int flag;
     
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for(i = 0; i < n; i++)
    {
         
        // If arr[i] and flag are equal
        if (arr[i] == flag)
        {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver code
public static void Main ()
{
    int[] arr = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = arr.Length;
    int k = 1;
     
    Console.Write(minSteps(arr, n, k));
}
}
 
// This code is contributed by chitranayal


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)