Open In App

Count of elements in array A left after performing deletion/rotation operation based on given conditions

Last Updated : 29 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two binary arrays, A[] and B[] of size N respectively, the task is to find the number of elements in array A[] that will be left after performing the following operation until no elements can be deleted:

  1. If the starting elements of array A[] and B[] are equal, then delete both the elements.
  2. Otherwise, append the starting character of array A[] to the end of the array, A[], after removing it.

Examples:

Input: A[] = {1, 1, 0, 1}, B[] = {1, 0, 1, 1}, N = 4 
Output:
Explanation:
The operations are performed as follows:

  1. A[0]( =1) = B[0]( =1): Delete the elements. Thereafter, the arrays are modified to {1, 0, 1} and {0, 1, 1} respectively.
  2. 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.
  3. A[0]( =0) = B[0]( =0): Delete the elements. Thereafter, the arrays are modified to {1, 1} and {1, 1} respectively.
  4. A[0]( =1) = B[0]( =1): Delete the elements. Thereafter, the arrays are modified to {1} and {1} respectively.
  5. 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:

Approach: The given problem can be solved by removing the common 0s and 1s, and then counting the unique number of 0s and 1s in both arrays. Consider the following observations:

  1. The elements can be deleted as long as there is an element equal to the first element of B[] left in the array A[].
  2. It can also be observed that the order of elements of A[] can be easily changed.
  3. Therefore, the idea is to keep the count of the number of 0s and 1s left in A[] and if an element is encountered in B[] such that the same element is no longer present in A[], then no more operations can be performed.

Follow the steps below to solve the problem:

  • Traverse the array, A[] and count the total number of 0s and 1s in variables and store them in variables, say zero and one respectively.
  • Initialize a variable say count as 0 to store the total number of deletions performed.
  • Traverse the array, B[] using the variable i and do the following:
    • If B[i] is equal to 0 and zero>0, then increment the value of count by 1 and decrement zero by 1.
    • Else, if B[i] is equal to 1 and one>0, then increment the value of count by 1 and decrement one by 1.
    • Otherwise, break out of the loop as no more operations can further be performed.
  • Finally, after completing the above steps, print the difference between N and count as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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




<script>
// Javascript program for the above approach
// Function to calculate minimum size
// of the array A[] after performing
// the given operations
function minimumSizeAfterDeletion(A, B, N)
{
     
    // Stores the count of 0s and 1s
    var zero = 0, one = 0;
 
    // Stores the total deletions performed
    var count = 0;
 
    // Traverse the array A[]
    for(var i = 0; i < N; i++)
    {
        if (A[i] == 0)
        {
            zero++;
        }
        else
        {
            one++;
        }
    }
 
    // Traverse array B[]
    for(var 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
    // Given Input
    var A = [ 1, 0, 1, 1, 1, 1 ];
    var B = [ 1, 1, 0, 1, 0, 1 ];
    var N = 6;
     
    // Function Call
    minimumSizeAfterDeletion(A, B, N);
    document.write(minimumSizeAfterDeletion(A, B, N));
 
// This code is contributed by shivanisinghss2110
</script>


Output

2

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time.
Auxiliary Space: O(1), as we are not using any extra space.



Similar Reads

Minimum possible sum of array elements after performing the given operation
Given an array arr[] of positive integers and an integer x, the task is to minimize the sum of elements of the array after performing the given operation at most once. In a single operation, any element from the array can be divided by x (if it is divisible by x) and at the same time, any other element from the array must be multiplied by x.Example
8 min read
Minimum possible sum of array elements after performing the given operation
Given an array arr[] of size N and a number X. If any sub array of the array(possibly empty) arr[i], arr[i+1], ... can be replaced with arr[i]/x, arr[i+1]/x, .... The task is to find the minimum possible sum of the array which can be obtained. Note: The given operation can only be performed once.Examples: Input: N = 3, X = 2, arr[] = {1, -2, 3} Out
9 min read
Maximize the value left after reducing the Arrays based on given conditions
Given three arrays arr1[], arr2[] and arr3[] of integers, the task is to find the maximum value left in an array after performing the following operation, where in each operation: Select an element (y) from one array and remove that from the array.Subtract y from another element(x) of another array. Note: If there are multiple occurrences of these
8 min read
Reduce the given Array of [1, N] by rotating left or right based on given conditions
Given a sorted array arr[] of the first N Natural Numbers and an integer X, the task is to print the last remaining element after performing the below operations (N - 1) times: If the value of X is 1, then right rotate the array by 1 unit and delete the last element.If the value of X is 2, then left rotate the array by 1 unit and delete the first e
6 min read
Sum of the updated array after performing the given operation
Given an array arr[] of N elements, the task is to update all the array elements such that an element arr[i] is updated as arr[i] = arr[i] - X where X = arr[i + 1] + arr[i + 2] + ... + arr[N - 1] and finally print the sum of the updated array.Examples: Input: arr[] = {40, 25, 12, 10} Output: 8 The updated array will be {-7, 3, 2, 10}. -7 + 3 + 2 +
8 min read
Maximum possible array sum after performing the given operation
Given an array arr[] of size N, the task is to find the maximum sum of the elements of the array after applying the given operation any number of times. In a single operation, choose an index 1 ? i &lt; N and multiply both arr[i] and arr[i - 1] by -1.Examples: Input: arr[] = {-10, 5, -4} Output: 19 Perform the operation for i = 1 and the array beco
9 min read
Count of distinct groups of strings formed after performing equivalent operation
Given an array arr[] of N strings consisting of lowercase alphabets, the task is to find the number of distinct groups of strings formed after performing the equivalent operation. Two strings are said to be equivalent if there exists the same character in both the strings and if there exists another string that is equivalent to one of the strings i
12 min read
Construct array B as last element left of every suffix array obtained by performing given operations on every suffix of given array
Given an array arr[] of N integers, the task is to print the last element left of every suffix array obtained by performing the following operation on every suffix of the array, arr[]: Copy the elements of the suffix array into an array suff[].Update ith suffix element as suff[i] = (suff[i] OR suff[i+1]) - (suff[i] XOR suff[i+1]) reducing the size
9 min read
Minimum element left from the array after performing given operations
Given an array arr[] of N integers, the task is to remove the elements from both the ends of the array i.e. in a single operation, either the first or the last element can be removed from the current remaining elements of the array. This operation needs to be performed in such a way that the last element left will have the minimum possible value. P
3 min read
Find the index which is the last to be reduced to zero after performing a given operation
Given an integer array arr[] of size N and an integer K, the task is to find the index which will be the last to be reduced to zero after performing a given operation. The operation is described as follows: Starting from arr[0] to arr[N - 1], update each element as arr[i] = arr[i] - K.If arr[i] &lt; K then set arr[i] = 0 and no further operation wi
6 min read