I am preparing for a job interview and have an question. I have 2 binary arrays of n and m size. I need to create an algorithm for merging them together and then separating them. The merged array is have to be also binary array. No information about the size of merged array, I assume it could be n+m.
-
4’merging’ according to which rule? simply append?MrSmith42– MrSmith422019-07-16 16:00:13 +00:00Commented Jul 16, 2019 at 16:00
-
3Adding an example would really lighten things up.MrSmith42– MrSmith422019-07-16 16:00:52 +00:00Commented Jul 16, 2019 at 16:00
-
I guess it is possible to append, but then how can I know where is the separation?DanM– DanM2019-07-16 16:01:07 +00:00Commented Jul 16, 2019 at 16:01
-
2Can you elaborate on what you mean by "merge" and "separate?"templatetypedef– templatetypedef2019-07-16 17:30:29 +00:00Commented Jul 16, 2019 at 17:30
Add a comment
|
1 Answer
If you know what is the maximum size of A and B, then you can code the sizes of A and B in binary, and create a new binary array by multiplexing
- size of
A Acontent- size if
B Bcontent
Then demultiplexing (separating A and B) is easy.
It is similar to what is performed in telecommunications.
Edit: I mentioned that maximum size must be known. This is because for demultiplexing we need to know how much bits are used to encode the sizes. Then, the number of bits for this encoding must be fixed.
1 Comment
Aldert
Great answer @Damien, I removed mine because yours is much better!