Minimize remaining array sizes by removing equal pairs of first array elements
Last Updated :
31 May, 2022
Given two binary arrays L1[] and L2[], each of size N, the task is to minimize the count of remaining array elements after performing the following operations:
- If the first element of both the arrays is same, then remove it from both the arrays.
- Otherwise, remove the first element from L1[] and append it at the end of the array L1[].
Examples:
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
Approach: The idea is to store the count of 1s and 0s in the array L1[]. Then, while traversing the array L2[], if 1 is encountered, decrement the count of 1s. Otherwise, decrement the count of 0s. At any instant, if either of the counts becomes less than 0, then this indicates that after that particular index, no further element can be removed. Follow the steps below to solve the problem:
- Traverse the array L1[] and count the number of 0s and 1s and store them in variables, say zero and one respectively.
- Now, traverse the array L2[] and perform the following steps:
- If the current element of L2[] is 1, then decrement one by 1. Otherwise, decrement zero by 1.
- If at any instant, either one or zero becomes negative, store the index in variable ans and break out of the loop.
- After completing the above steps, print the value of (N – ans) as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countRemainingElements(
int L1[], int L2[], int n)
{
int one = 0;
int zero = 0;
for ( int i = 0; i < n; i++) {
if (L1[i] == 1)
one++;
else
zero++;
}
int ans = n;
for ( int i = 0; i < n; i++) {
if (L2[i] == 1) {
one--;
if (one < 0) {
ans = i;
break ;
}
}
else {
zero--;
if (zero < 0) {
ans = i;
break ;
}
}
}
cout << n - ans;
}
int main()
{
int L1[] = { 1, 1, 0, 0 };
int L2[] = { 0, 0, 0, 1 };
int N = sizeof (L1) / sizeof (L1[0]);
countRemainingElements(L1, L2, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void countRemainingElements( int [] L1,
int [] L2,
int n)
{
int one = 0 ;
int zero = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (L1[i] == 1 )
one++;
else
zero++;
}
int ans = n;
for ( int i = 0 ; i < n; i++)
{
if (L2[i] == 1 )
{
one--;
if (one < 0 )
{
ans = i;
break ;
}
}
else
{
zero--;
if (zero < 0 )
{
ans = i;
break ;
}
}
}
System.out.println(n - ans);
}
public static void main(String[] args)
{
int [] L1 = { 1 , 1 , 0 , 0 };
int [] L2 = { 0 , 0 , 0 , 1 };
int N = L1.length;
countRemainingElements(L1, L2, N);
}
}
|
C#
using System;
class GFG{
static void countRemainingElements( int [] L1,
int [] L2,
int n)
{
int one = 0;
int zero = 0;
for ( int i = 0; i < n; i++)
{
if (L1[i] == 1)
one++;
else
zero++;
}
int ans = n;
for ( int i = 0; i < n; i++)
{
if (L2[i] == 1)
{
one--;
if (one < 0)
{
ans = i;
break ;
}
}
else
{
zero--;
if (zero < 0)
{
ans = i;
break ;
}
}
}
Console.WriteLine(n - ans);
}
static public void Main()
{
int [] L1 = { 1, 1, 0, 0 };
int [] L2 = { 0, 0, 0, 1 };
int N = L1.Length;
countRemainingElements(L1, L2, N);
}
}
|
Python3
def countRemainingElements(L1, L2, n):
one = 0 ;
zero = 0 ;
for i in range (n):
if (L1[i] = = 1 ):
one + = 1 ;
else :
zero + = 1 ;
ans = n;
for i in range (n):
if (L2[i] = = 1 ):
one - = 1 ;
if (one < 0 ):
ans = i;
break ;
else :
zero - = 1 ;
if (zero < 0 ):
ans = i;
break ;
print (n - ans);
if __name__ = = '__main__' :
L1 = [ 1 , 1 , 0 , 0 ];
L2 = [ 0 , 0 , 0 , 1 ];
N = len (L1);
countRemainingElements(L1, L2, N);
|
Javascript
<script>
function countRemainingElements( L1, L2, n)
{
let one = 0;
let zero = 0;
for (let i = 0; i < n; i++)
{
if (L1[i] == 1)
one++;
else
zero++;
}
let ans = n;
for (let i = 0; i < n; i++)
{
if (L2[i] == 1)
{
one--;
if (one < 0)
{
ans = i;
break ;
}
}
else
{
zero--;
if (zero < 0)
{
ans = i;
break ;
}
}
}
document.write(n - ans);
}
let L1 = [ 1, 1, 0, 0 ];
let L2 = [ 0, 0, 0, 1 ];
let N = L1.length;
countRemainingElements(L1, L2, N);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...