📌  相关文章
📜  根据给定条件执行删除/旋转操作后数组A中元素的计数

📅  最后修改于: 2021-10-27 07:28:31             🧑  作者: Mango

给定两个大小分别为N 的二进制数组A[]B[] ,任务是找出数组A[]中在执行以下操作后将剩余的元素数,直到没有元素可以被删除:

  1. 如果数组A[]B[]的起始元素相等,则删除这两个元素。
  2. 否则,在删除数组A[]后,将数组A[]的起始字符附加到数组的末尾。

例子:

方法:给定的问题可以通过去除常见的 0 和 1 来解决,然后计算两个数组中 0 和 1 的唯一数量。考虑以下观察结果:

  1. 只要数组A[] 中剩余的元素等于B[]的第一个元素,就可以删除这些元素。
  2. 还可以观察到, A[]的元素顺序可以很容易地改变。
  3. 因此,这个想法是保留A[] 中剩余的01的数量,如果在B[] 中遇到一个元素,使得相同的元素不再存在于A[] 中,则不再存在可以进行操作。

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

  • 遍历数组A[]并计算变量中01的总数并将它们存储在变量中,分别说为零
  • 初始化一个变量说count as 0 存储执行的删除总数。
  • 使用变量i遍历数组B[]并执行以下操作:
    • 如果B[i]等于0zero>0 ,则将count的值增加1并将减少1
    • 否则,如果B [i]等于1一个> 0,则通过1和递减一个递增1计数的值。
    • 否则,退出循环,因为无法进一步执行更多操作。
  • 最后,完成上述步骤后,打印Ncount的差值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate minimum size
// of the array A[] after performing
// the given operations
int minimumSizeAfterDeletion(int A[], int B[], int N)
{
    // Stores the count of 0s and 1s
    int zero = 0, one = 0;
 
    // Stores the total deletions performed
    int count = 0;
 
    // Traverse the array A[]
    for (int i = 0; i < N; i++) {
        if (A[i] == 0) {
            zero++;
        }
        else {
            one++;
        }
    }
 
    // Traverse array B[]
    for (int i = 0; i < N; i++) {
 
        // If the B[i] is 0 and zero is
        // greater than 0
        if (B[i] == 0 && zero > 0) {
            // Increment count by 1
            count++;
            // Decrement zero by 1
            zero--;
        }
 
        // Else if the B[i] is 1 and one is
        // greater than 0
        else if (B[i] == 1 && one > 0) {
            // Increment count by 1
            count++;
            // Decrement one by 1
            one--;
        }
 
        // Otherwise
        else {
            break;
        }
    }
 
    // Return the answer
    return N - count;
}
 
// Driver Code
int main()
{
 
    // Given Input
    int A[] = { 1, 0, 1, 1, 1, 1 };
    int B[] = { 1, 1, 0, 1, 0, 1 };
    int N = 6;
 
    // Function Call
    cout << minimumSizeAfterDeletion(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to calculate minimum size
// of the array A[] after performing
// the given operations
static int minimumSizeAfterDeletion(int A[], int B[],
                                    int N)
{
     
    // Stores the count of 0s and 1s
    int zero = 0, one = 0;
 
    // Stores the total deletions performed
    int count = 0;
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
        if (A[i] == 0)
        {
            zero++;
        }
        else
        {
            one++;
        }
    }
 
    // Traverse array B[]
    for(int i = 0; i < N; i++)
    {
         
        // If the B[i] is 0 and zero is
        // greater than 0
        if (B[i] == 0 && zero > 0)
        {
             
            // Increment count by 1
            count++;
             
            // Decrement zero by 1
            zero--;
        }
 
        // Else if the B[i] is 1 and one is
        // greater than 0
        else if (B[i] == 1 && one > 0)
        {
             
            // Increment count by 1
            count++;
             
            // Decrement one by 1
            one--;
        }
 
        // Otherwise
        else
        {
            break;
        }
    }
 
    // Return the answer
    return N - count;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    int A[] = { 1, 0, 1, 1, 1, 1 };
    int B[] = { 1, 1, 0, 1, 0, 1 };
    int N = 6;
     
    // Function Call
    minimumSizeAfterDeletion(A, B, N);
    System.out.println(minimumSizeAfterDeletion(A, B, N));
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to calculate minimum size
# of the array A[] after performing
# the given operations
def minimumSizeAfterDeletion(A, B, N):
     
    # Stores the count of 0s and 1s
    zero = 0
    one = 0
     
    # Stores the total deletions performed
    count = 0
     
    # Traverse the array A[]
    for i in range(N):
        if A[i] == 0:
            zero += 1
        else:
            one += 1
     
    # Traverse array B[]       
    for i in range(N):
         
        # If the B[i] is 0 and zero is
        # greater than 0
        if B[i] == 0 and zero > 0:
             
            # Increment count by 1
            count += 1
             
            # Decrement zero by 1
            zero -= 1
         
        # Else if the B[i] is 1 and one is
        # greater than 0
        elif B[i] == 1 and one > 0:
             
            # Increment count by 1
            count += 1
             
            # Decrement one by 1
            one -= 1
             
        # Otherwise
        else:
            break
     
    # Return the answer   
    return N - count
 
# Driver code
 
# Given input
A = [ 1, 0, 1, 1, 1, 1 ]
B = [ 1, 1, 0, 1, 0, 1 ]
N = 6
 
# Function call
print(minimumSizeAfterDeletion(A, B, N))
 
# This code is contributed by Parth Manchanda


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate minimum size
// of the array A[] after performing
// the given operations
static int minimumSizeAfterDeletion(int []A, int []B, int N)
{
    // Stores the count of 0s and 1s
    int zero = 0, one = 0;
 
    // Stores the total deletions performed
    int count = 0;
 
    // Traverse the array A[]
    for (int i = 0; i < N; i++) {
        if (A[i] == 0) {
            zero++;
        }
        else {
            one++;
        }
    }
 
    // Traverse array B[]
    for (int i = 0; i < N; i++) {
 
        // If the B[i] is 0 and zero is
        // greater than 0
        if (B[i] == 0 && zero > 0) {
            // Increment count by 1
            count++;
            // Decrement zero by 1
            zero--;
        }
 
        // Else if the B[i] is 1 and one is
        // greater than 0
        else if (B[i] == 1 && one > 0) {
            // Increment count by 1
            count++;
            // Decrement one by 1
            one--;
        }
 
        // Otherwise
        else {
            break;
        }
    }
 
    // Return the answer
    return N - count;
}
 
// Driver Code
public static void Main()
{
 
    // Given Input
    int []A = { 1, 0, 1, 1, 1, 1 };
    int []B = { 1, 1, 0, 1, 0, 1 };
    int N = 6;
 
    // Function Call
    Console.Write(minimumSizeAfterDeletion(A, B, N));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程