In an interview, I was asked the following question, and I could not find a solution.
This array is an array of character length n and the "important section" (all characters in this section must be stored) length m , where n> = m> = 0 as follows:

Without additional space, complete the following process:
Delete all occurrences of A and duplicate all occurrences of B , return an additional array of mutated array. For example, for the above array [C,A,X,B,B,F,Q] n = 7, m = 5, the output will be [C,X,B,B,B,B] . Note that the length of the mutated array is 6, since Q is in the backup partition and B is duplicated.
Return -1 if the operation cannot be completed.
Examples:
n=2, m=2 , [A,B] => [B,B] n=2, m=2 , [B,B] => -1 (since the result [B,B,B,B] is larger then the array) n=3, m=2 , [A,B,C] => [B,B] n=3, m=3 , [A,B,C] => [B,B,C] n=3, m=2 , [Z,B,A] => [Z,B,B] (since A was in the redundant section)
We are looking for an example code. Can this be done in O (n) time complexity?