Interleave array in constant space

Matt picture Matt · Nov 22, 2009 · Viewed 9k times · Source

I ran across the following sample job interview question. How can I solve it?

Suppose we have an array a1, a2,... , an, b1, b2, ..., bn.

The goal is to change this array to a1, b1, a2, b2, ..., an, bn in O(n) time and in O(1) space. In other words, we need a linear-time algorithm to modify the array in place, with no more than a constant amount of extra storage.

Answer

Anony picture Anony · Jan 17, 2010

This problem is not as trivial as people make out to be. Homework? LOL.

The following link has a solution: http://arxiv.org/abs/0805.1598