给定两个大小分别为N 的二进制数组A[]和B[] ,任务是找出数组A[]中在执行以下操作后将剩余的元素数,直到没有元素可以被删除:
- 如果数组A[]和B[]的起始元素相等,则删除这两个元素。
- 否则,在删除数组A[]后,将数组A[]的起始字符附加到数组的末尾。
例子:
Input: A[] = {1, 1, 0, 1}, B[] = {1, 0, 1, 1}, N = 4
Output: 0
Explanation:
The operations are performed as follows:
- A[0]( =1) = B[0]( =1): Delete the elements. Thereafter, the arrays are modified to {1, 0, 1} and {0, 1, 1} respectively.
- A[0](=1) != B[0](= 0): Shift the A[0] to the end of the array A[]. Thereafter, the arrays are modified to { 0, 1, 1} and {0, 1, 1} respectively.
- A[0]( =0) = B[0]( =0): Delete the elements. Thereafter, the arrays are modified to {1, 1} and {1, 1} respectively.
- A[0]( =1) = B[0]( =1): Delete the elements. Thereafter, the arrays are modified to {1} and {1} respectively.
- A[0]( =1) = B[0]( =1): Delete the elements. Thereafter, both arrays became empty.
Therefore, no elements are left in the array A[].
Input: A[] = {1, 0, 1, 1, 1, 1}, B[] = {1, 1, 0, 1, 0, 1}, N = 6
Output: 2
方法:给定的问题可以通过去除常见的 0 和 1 来解决,然后计算两个数组中 0 和 1 的唯一数量。考虑以下观察结果:
- 只要数组A[] 中剩余的元素等于B[]的第一个元素,就可以删除这些元素。
- 还可以观察到, A[]的元素顺序可以很容易地改变。
- 因此,这个想法是保留A[] 中剩余的0和1的数量,如果在B[] 中遇到一个元素,使得相同的元素不再存在于A[] 中,则不再存在可以进行操作。
请按照以下步骤解决问题:
- 遍历数组A[]并计算变量中0和1的总数并将它们存储在变量中,分别说为零和一。
- 初始化一个变量说count as 0 存储执行的删除总数。
- 使用变量i遍历数组B[]并执行以下操作:
- 如果B[i]等于0且zero>0 ,则将count的值增加1并将零减少1 。
- 否则,如果B [i]等于1且一个> 0,则通过1和递减一个递增1计数的值。
- 否则,退出循环,因为无法进一步执行更多操作。
- 最后,完成上述步骤后,打印N与count的差值作为结果。
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。