Algorithm Question Tag #1

One day at summer holidays, there was a girl feeling really surprised since her mom gave her lots of cakes with variation in size. Then, she measured its size one by one and signed the size on top of each cake. The measurement was arbitrary so there were no exact unit how she measured it. The number were 20, 11, 50, 28, 35, 30, and 43.

However, suddenly she was curious and wanted to arrange the cakes with alternating order of size. Suppose, for example, the order is 20, 50, 11, 30, 28, 43, and 35, so that the first number is lower than the second one, but the second number is higher than the third one, but the third is lower than the forth, and so forth.

Formally, if we have the order like a1, a2, a3, a4, a5, then the order will have to be b1<=b2>=b3<=b4>=b5 and so on.

Sampe Input: 20 11 50 28 35 30 43
Sample Output: 20 50 11 30 28 43 35

So please help the girl to find the solution by giving your algorithm 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s