Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in-place permutation of a array follows this rule

Tags:

Suppose there is an array, we want to find everything in the odd index (index starting with 0), and move it to the end. Everything in the even index move it to the beginning. The relative order of all odd index items and all even index items are preserved.

i.e. if the array is

a1 b1 a2 b2 ...  an bn    

after the operation it becomes

a1 a2 a3 ... an b1 b2 ... bn

Can this be done in-place and in O(n) time?