给定两个二进制数组L1[]和L2[] ,每个数组的大小为N ,任务是在执行以下操作后最小化剩余数组元素的数量:
- 如果两个数组的第一个元素相同,则将其从两个数组中删除。
- 否则,从L1[] 中删除第一个元素并将其附加到数组L1[]的末尾。
例子:
Input: L1 = {1, 0, 1, 0}, L2 = {0, 1, 0, 1}
Output: 0
Explanation:
L1[0] = 1, L2[0] = 0. Therefore, L1[] modifies to {0, 1, 0, 1}.
Since L1[0] and L2[0] are equal, both are removed from their respective arrays.
Now, L1[] modifies to {1, 0, 1} and L2 modifies to {1, 0, 1}.
For the next three steps, the first array element are equal. Therefore, the count of remaining elements is 0.
Input: L1 = {1, 1, 0, 0}, L2 = {0, 0, 0, 1}
Output: 2
方法:想法是将1和0的计数存储在数组L1[] 中。然后,在遍历数组L2[] 时,如果遇到1 ,则递减1的计数。否则,减少0秒的计数。在任何时候,如果任何一个计数变得小于0 ,那么这表明在该特定索引之后,无法删除更多元素。请按照以下步骤解决问题:
- 遍历数组L1[]并计算0和1的数量并将它们存储在变量中,分别说为零和一。
- 现在,遍历数组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;
# Print the 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live