Suppose I have the following array:
1, 4, 5, 2, 3
I need to rearrange it to
5, 1, 4, 2, 3
There is only on extra space; one int.
I figured one solution to solve it. But it is O(n^2) complexity.
Can anyone offer a faster solution?
Thanks
Edited:
sorry, not rotating array.
I need to change original array to result array.
The order can be arbitrary.
I just need to make A -> B.
B is been told to me.
thanks
Edited 2″
Make it clearer.
Array B is not fixed.
We need to find a general solution for this.
Updated:
Thank you all. Seems like this is a brain teaser question. haha 😀
My friend been asked by Amazon interviewer for this. LOL
This solution works using O(1) space.
Without using python’s tuple packing, the value of one of source[i] or source[source.index(dest[i])] could be stored in an int.