Wednesday, April 13, 2011

Pairing


Given an array of size 2N, such that its elements are as follows:
{a1,a2,a3,...,an,b1,b2,b3...,bn}
You have to rearrange this array, without using any extra array such that resulting array would be:
{a1,b1,a2,b2,...,an,bn}


1. Swap a2,b1 ;  a4,b3...so that pairs  a1b1,a3b3,a2b2,a4b4 are created.
2. Now swap the pairs a3b3 with a2b2

No comments: