📌  相关文章
📜  来自奇数长度子数组的奇数索引元素的最小翻转,以使两个给定数组相等

📅  最后修改于: 2021-09-04 07:51:18             🧑  作者: Mango

给定两个大小为N 的二进制数组X[]Y[] ,任务是通过选择任何奇数长度的子数组并翻转所有奇数索引元素的最少操作次数将数组X[]转换为数组Y[]子阵列。

例子:

方法:这个想法是分别计算偶数和奇数位置的操作。请按照以下步骤解决问题:

  • 将变量C初始化为0以存储操作计数。
  • 在奇数位置遍历数组X[]元素并取计数器count = 0。
    • 检查X[i]Y[i]之间的连续不相等元素,并将计数器计数每次增加1
    • X[i]Y[i]相等时,增加全局C以将操作增加 1,因为在该操作中,所有奇数位置都可以与Y 中的一样
  • 类似地,在偶数位置上遍历数组X[]元素并再次取计数器count = 0。
    • 检查X[i]Y[i]之间的连续不相等元素,并将计数器计数每次增加1
    • X[i]Y[i]相等时,增加全局计数器C以将操作增加1
  • 完成上述步骤后,打印C的值作为所需操作的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum flip
// of subarrays required at alternate
// index to make binary arrays equals
void minOperation(int X[], int Y[],
                  int n)
{
    // Stores count of total operations
    int C = 0;
 
    // Stores count of consecutive
    // unequal elements
    int count = 0;
 
    // Loop to run on odd positions
    for (int i = 1; i < n; i = i + 2) {
 
        if (X[i] != Y[i]) {
            count++;
        }
        else {
 
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
 
            // Change count to 0
            count = 0;
        }
    }
 
    // If all last elements are equal
    if (count != 0)
        C++;
 
    count = 0;
 
    // Loop to run on even positions
    for (int i = 0; i < n; i = i + 2) {
 
        if (X[i] != Y[i]) {
            count++;
        }
        else {
 
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
 
            // Change count to 0
            count = 0;
        }
    }
 
    if (count != 0)
        C++;
 
    // Print the minimum operations
    cout << C;
}
 
// Driver Code
int main()
{
    int X[] = { 1, 0, 0, 0, 0, 1 };
    int Y[] = { 1, 1, 0, 1, 1, 1 };
    int N = sizeof(X) / sizeof(X[0]);
 
    // Function Call
    minOperation(X, Y, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to find the minimum flip
// of subarrays required at alternate
// index to make binary arrays equals
static void minOperation(int X[], int Y[],
                         int n)
{
     
    // Stores count of total operations
    int C = 0;
  
    // Stores count of consecutive
    // unequal elements
    int count = 0;
  
    // Loop to run on odd positions
    for(int i = 1; i < n; i = i + 2)
    {
         
        if (X[i] != Y[i])
        {
            count++;
        }
        else
        {
             
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
  
            // Change count to 0
            count = 0;
        }
    }
  
    // If all last elements are equal
    if (count != 0)
        C++;
  
    count = 0;
  
    // Loop to run on even positions
    for(int i = 0; i < n; i = i + 2)
    {
        if (X[i] != Y[i])
        {
            count++;
        }
        else
        {
             
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
  
            // Change count to 0
            count = 0;
        }
    }
  
    if (count != 0)
        C++;
  
    // Print the minimum operations
    System.out.print(C);
}
    
// Driver Code
public static void main(String[] args)
{
    int X[] = { 1, 0, 0, 0, 0, 1 };
    int Y[] = { 1, 1, 0, 1, 1, 1 };
    int N = X.length;
  
    // Function Call
    minOperation(X, Y, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program for the above approach
 
# Function to find the minimum flip
# of subarrays required at alternate
# index to make binary arrays equals
def minOperation(X, Y, n):
   
    # Stores count of total operations
    C = 0;
 
    # Stores count of consecutive
    # unequal elements
    count = 0;
 
    # Loop to run on odd positions
    for i in range(1, n, 2):
 
        if (X[i] != Y[i]):
            count += 1;
        else:
 
            # Incrementing the
            # global counter
            if (count != 0):
                C += 1;
 
            # Change count to 0
            count = 0;
 
    # If all last elements are equal
    if (count != 0):
        C += 1;
 
    count = 0;
 
    # Loop to run on even positions
    for i in range(0, n, 2):
        if (X[i] != Y[i]):
            count += 1;
        else:
 
            # Incrementing the
            # global counter
            if (count != 0):
                C += 1;
 
            # Change count to 0
            count = 0;
 
    if (count != 0):
        C += 1;
 
    # Prthe minimum operations
    print(C);
 
# Driver Code
if __name__ == '__main__':
    X = [1, 0, 0, 0, 0, 1];
    Y = [1, 1, 0, 1, 1, 1];
    N = len(X);
 
    # Function Call
    minOperation(X, Y, N);
 
    # This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
    
// Function to find the minimum flip
// of subarrays required at alternate
// index to make binary arrays equals
static void minOperation(int []X, int []Y,
                         int n)
{
     
    // Stores count of total operations
    int C = 0;
  
    // Stores count of consecutive
    // unequal elements
    int count = 0;
  
    // Loop to run on odd positions
    for(int i = 1; i < n; i = i + 2)
    {
        if (X[i] != Y[i])
        {
            count++;
        }
        else
        {
             
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
  
            // Change count to 0
            count = 0;
        }
    }
  
    // If all last elements are equal
    if (count != 0)
        C++;
  
    count = 0;
  
    // Loop to run on even positions
    for(int i = 0; i < n; i = i + 2)
    {
        if (X[i] != Y[i])
        {
            count++;
        }
        else
        {
             
            // Incrementing the
            // global counter
            if (count != 0)
                C++;
  
            // Change count to 0
            count = 0;
        }
    }
  
    if (count != 0)
        C++;
  
    // Print the minimum operations
    Console.Write(C);
}
    
// Driver Code
public static void Main(String[] args)
{
    int []X = { 1, 0, 0, 0, 0, 1 };
    int []Y = { 1, 1, 0, 1, 1, 1 };
    int N = X.Length;
     
    // Function Call
    minOperation(X, Y, N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live