Transpose matrix stored in a 1-dimensional array without using extra memory

Viewed 3562

Possible Duplicate:
In-place transposition of a matrix

Recently attended an Technical Written Interview. Came through the following question.

I have an array as say

testArray = {a1,a2,a3,...an,b1,b2,b3,....bn,c1,c2,c3,.....,cn}

I need to sort this array as `

testArray = {a1,b1,c1,a2,b2,c2,a3,b3,c3,.....,an,bn,cn}

Constraint is I should not use extra memory, should not use any inbuilt function. Should write complete code, it can be in any language and can also use any data structure.

eg:

Input: {1,2,3,4,5,6,7,8,9}, n = 3

Output: {1,4,7,2,5,8,3,6,9}

I could not get any solution within the constraint, can anyone provide solution or suggestion?

4 Answers
Related