Greedy approach works here. Its a simple O(N) solution. Just swap the adjacent numbers if they don't follow the given rule. Eventually this algorithm will generate correct output. I just don't want to write the complete explanation of why it works :P. Its a famous ques. called "Wiggle Sort". Google it.