📌  相关文章
📜  通过删除相等的第一对数组元素来最小化剩余的数组大小

📅  最后修改于: 2021-04-22 00:34:22             🧑  作者: Mango

给定两个二进制数组L1 []L2 [] ,每个数组的大小为N ,任务是在执行以下操作后,将剩余数组元素的数量减至最少:

  • 如果两个数组的第一个元素相同,则将其从两个数组中删除。
  • 否则,从L1 []中删除第一个元素,并将其附加在数组L1 []的末尾。

例子:

方法:这个想法是将1 s和0 s的计数存储在数组L1 []中。然后,在遍历数组L2 []时,如果遇到1 ,则将计数减少1 s。否则,将计数递减0 s。在任何时刻,如果任何一个计数小于0 ,则表明在该特定索引之后,无法再删除其他元素。请按照以下步骤解决问题:

  • 遍历数组L1 []并计算0 s和1 s的数目,并将它们存储在变量中,分别为零
  • 现在,遍历数组L2 []并执行以下步骤:
    • 如果L2 []的当前元素为1,则递减一个1。否则,将1
    • 如果在任何时刻,一个变为负数,则将索引存储在变量ans中,并退出循环。
  • 完成上述步骤后,将(N – ans)的值打印为所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the remaining
// elements in the arrays
void countRemainingElements(
    int L1[], int L2[], int n)
{
    // Stores the count of 1s in L1[]
    int one = 0;
 
    // Store the count of 0s in L2[]
    int zero = 0;
 
    // Traverse the array L1[]
    for (int i = 0; i < n; i++) {
 
        // If condition is true
        if (L1[i] == 1)
 
            // Increment one by 1
            one++;
        else
 
            // Increment zero by 1
            zero++;
    }
 
    // Stores the index after which no
    // further removals are possible
    int ans = n;
 
    // Traverse the array L2[]
    for (int i = 0; i < n; i++) {
 
        // If condition is true
        if (L2[i] == 1) {
 
            // Decrement one by 1
            one--;
 
            // If one < 0, then break
            // out of the loop
            if (one < 0) {
                ans = i;
                break;
            }
        }
        else {
 
            // Decrement zero by 1
            zero--;
 
            // If zero < 0, then
            // break out of loop
            if (zero < 0) {
                ans = i;
                break;
            }
        }
    }
 
    // Print the answer
    cout << n - ans;
}
 
// Driver Code
int main()
{
    int L1[] = { 1, 1, 0, 0 };
    int L2[] = { 0, 0, 0, 1 };
    int N = sizeof(L1) / sizeof(L1[0]);
 
    // Function Call
    countRemainingElements(L1, L2, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the remaining
// elements in the arrays
static void countRemainingElements(int[] L1,
                                   int[] L2,
                                   int n)
{
     
    // Stores the count of 1s in L1[]
    int one = 0;
 
    // Store the count of 0s in L2[]
    int zero = 0;
 
    // Traverse the array L1[]
    for(int i = 0; i < n; i++)
    {
         
        // If condition is true
        if (L1[i] == 1)
 
            // Increment one by 1
            one++;
        else
 
            // Increment zero by 1
            zero++;
    }
 
    // Stores the index after which no
    // further removals are possible
    int ans = n;
 
    // Traverse the array L2[]
    for(int i = 0; i < n; i++)
    {
         
        // If condition is true
        if (L2[i] == 1)
        {
             
            // Decrement one by 1
            one--;
 
            // If one < 0, then break
            // out of the loop
            if (one < 0)
            {
                ans = i;
                break;
            }
        }
        else
        {
             
            // Decrement zero by 1
            zero--;
 
            // If zero < 0, then
            // break out of loop
            if (zero < 0)
            {
                ans = i;
                break;
            }
        }
    }
 
    // Print the answer
    System.out.println(n - ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int[] L1 = { 1, 1, 0, 0 };
    int[] L2 = { 0, 0, 0, 1 };
    int N = L1.length;
 
    // Function Call
    countRemainingElements(L1, L2, N);
}
}
 
// This code is contributed by Dharanendra L V


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the remaining
// elements in the arrays
static void countRemainingElements(int[] L1,
                                   int[] L2,
                                   int n)
{
     
    // Stores the count of 1s in L1[]
    int one = 0;
 
    // Store the count of 0s in L2[]
    int zero = 0;
 
    // Traverse the array L1[]
    for(int i = 0; i < n; i++)
    {
         
        // If condition is true
        if (L1[i] == 1)
 
            // Increment one by 1
            one++;
        else
 
            // Increment zero by 1
            zero++;
    }
 
    // Stores the index after which no
    // further removals are possible
    int ans = n;
 
    // Traverse the array L2[]
    for(int i = 0; i < n; i++)
    {
         
        // If condition is true
        if (L2[i] == 1)
        {
             
            // Decrement one by 1
            one--;
 
            // If one < 0, then break
            // out of the loop
            if (one < 0)
            {
                ans = i;
                break;
            }
        }
        else
        {
             
            // Decrement zero by 1
            zero--;
 
            // If zero < 0, then
            // break out of loop
            if (zero < 0)
            {
                ans = i;
                break;
            }
        }
    }
 
    // Print the answer
    Console.WriteLine(n - ans);
}
 
// Driver Code
static public void Main()
{
    int[] L1 = { 1, 1, 0, 0 };
    int[] L2 = { 0, 0, 0, 1 };
    int N = L1.Length;
 
    // Function Call
    countRemainingElements(L1, L2, N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to count the remaining
# elements in the arrays
def countRemainingElements(L1, L2, n):
 
    # Stores the count of 1s in L1
    one = 0;
 
    # Store the count of 0s in L2
    zero = 0;
 
    # Traverse the array L1
    for i in range(n):
 
        # If condition is True
        if (L1[i] == 1):
 
            # Increment one by 1
            one += 1;
        else:
 
            # Increment zero by 1
            zero += 1;
     
 
    # Stores the index after which no
    # further removals are possible
    ans = n;
 
    # Traverse the array L2
    for i in range(n):
 
        # If condition is True
        if (L2[i] == 1):
 
            # Decrement one by 1
            one -= 1;
 
            # If one < 0, then break
            # out of the loop
            if (one < 0):
                ans = i;
                break;           
        else:
 
            # Decrement zero by 1
            zero -= 1;
 
            # If zero < 0, then
            # break out of loop
            if (zero < 0):
                ans = i;
                break;
 
    # Prthe answer
    print(n - ans);
 
# Driver Code
if __name__ == '__main__':
    L1 = [ 1, 1, 0, 0 ];
    L2 = [ 0, 0, 0, 1 ];
    N = len(L1);
 
    # Function Call
    countRemainingElements(L1, L2, N);
 
# This code is contributed by gauravrajput1


Javascript


输出:
2

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