Removing Subsequence from concatenated Array
Last Updated :
09 Sep, 2023
Given array A[] formed by the concatenation of string “ABC” one or multiple times. The task for this problem is to remove all occurrences of subsequence “ABC” by performing the following operation a minimum number of times. In one operation swap any two elements of array A[]. print the number of operations required and elements on which operations are performed in each step.
Examples:
Input: A[] = {‘A’, ‘B’, ‘C’}
Output: 1
1 3
Explanation: Swapping 1’st and 3’rd element of array A[] it becomes A[] = {‘C’, ‘B’, ‘A’} which does not contain “ABC'” as subsequence.
Input: A[] = {‘A’, ‘B’, ‘C’, ‘A’, ‘B’, ‘C’};
Output: 1
1 6
Approach: To solve the problem follow the below idea:
No subsequences of string “ABC” would also mean no substrings of “ABC” in array A[]. it would be also be the lower bound for having no subsequences of string “ABC”.
Swap i-th A from start with i-th C from end for 1 <= i <= ceil(N / 6) We can see that, no substrings of “ABC” exists after performing ceil(N / 6) operations. Since we can only destroy atmost 2 substrings in one operations, ceil(N / 6) is minimum possible. Now if you see clearly, after performing above operations, there does not exist any subsequence of string ABC in original string. Hence ceil(N / 6) is also the answer for the original problem.
Below are the steps for the above approach:
- Create variable numberOfOperations.
- Create two pointers L = 1 and R = N.
- Create an array eachStep[][2] to store which elements will be replaced at which step.
- Run a while loop till L < R.
- Each step push {L, R} to eachStep[][2] then move pointer L to 3 steps forwards and R to 3 steps backward.
- After while loop ends print the numberOfOperations and eachStep[][2] array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimizeOperations( char A[], int N)
{
int numberOfOperations = ceil (N / 2.0);
int L = 1, R = N;
vector<pair< int , int > > eachStep;
while (L < R) {
eachStep.push_back({ L, R });
L += 3;
R -= 3;
}
cout << numberOfOperations << endl;
for ( auto ans : eachStep)
cout << ans.first << " " << ans.second << endl;
cout << endl
<< endl;
}
int32_t main()
{
int N = 3;
char A[] = { 'A' , 'B' , 'C' };
minimizeOperations(A, N);
int N1 = 6;
char A1[] = { 'A' , 'B' , 'C' , 'A' , 'B' , 'C' };
minimizeOperations(A1, N1);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static void minimizeOperations( char [] A, int N)
{
int numberOfOperations = ( int )Math.ceil(N / 2.0 );
int L = 1 , R = N;
ArrayList<ArrayList<Integer> > eachStep
= new ArrayList<>();
while (L < R) {
ArrayList<Integer> step = new ArrayList<>();
step.add(L);
step.add(R);
eachStep.add(step);
L += 3 ;
R -= 3 ;
}
System.out.println(numberOfOperations);
for (ArrayList<Integer> ans : eachStep)
System.out.println(ans.get( 0 ) + " "
+ ans.get( 1 ));
System.out.println( "\n\n" );
}
public static void main(String[] args)
{
int N = 3 ;
char [] A = { 'A' , 'B' , 'C' };
minimizeOperations(A, N);
int N1 = 6 ;
char [] A1 = { 'A' , 'B' , 'C' , 'A' , 'B' , 'C' };
minimizeOperations(A1, N1);
}
}
|
Python3
def minimize_operations(A):
N = len (A)
number_of_operations = (N + 1 ) / / 2
L, R = 1 , N
each_step = []
while L < R:
each_step.append((L, R))
L + = 3
R - = 3
print (number_of_operations)
for ans in each_step:
print (ans[ 0 ], ans[ 1 ])
print ( "\n\n" )
if __name__ = = "__main__" :
A = [ 'A' , 'B' , 'C' ]
minimize_operations(A)
A1 = [ 'A' , 'B' , 'C' , 'A' , 'B' , 'C' ]
minimize_operations(A1)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void minimizeOperations( char [] A, int N)
{
int numberOfOperations = ( int )Math.Ceiling(N / 2.0);
int L = 1, R = N;
List<List< int >> eachStep = new List<List< int >>();
while (L < R)
{
List< int > step = new List< int >();
step.Add(L);
step.Add(R);
eachStep.Add(step);
L += 3;
R -= 3;
}
Console.WriteLine(numberOfOperations);
foreach (List< int > ans in eachStep)
{
Console.WriteLine(ans[0] + " " + ans[1]);
}
Console.WriteLine( "\n\n" );
}
public static void Main( string [] args)
{
int N = 3;
char [] A = { 'A' , 'B' , 'C' };
minimizeOperations(A, N);
int N1 = 6;
char [] A1 = { 'A' , 'B' , 'C' , 'A' , 'B' , 'C' };
minimizeOperations(A1, N1);
}
}
|
Javascript
function minimizeOperations(A, N) {
const numberOfOperations = Math.ceil(N / 2);
let L = 1;
let R = N;
const eachStep = [];
while (L < R) {
eachStep.push([L, R]);
L += 3;
R -= 3;
}
console.log(numberOfOperations);
eachStep.forEach((ans) => console.log(ans[0], ans[1]));
console.log( '\n\n' );
}
function main() {
const N = 3;
const A = [ 'A' , 'B' , 'C' ];
minimizeOperations(A, N);
const N1 = 6;
const A1 = [ 'A' , 'B' , 'C' , 'A' , 'B' , 'C' ];
minimizeOperations(A1, N1);
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...