3 a1 >= a2 <= a3 >= a4 <= a5..... Country: United States 9 of 11 vote There are two O(n) solutions for this problem that does not require ordering. 1. You can find the median in O(n) and then rearrange the elements around the median 2. (Better Solution) If you notice the desired ordering, the even numbered elements are bigger (or equal) than the next element, and the odd numbered elements are less than (or equal) than the next element, of course I am assuming the array is 0 offset. So, you can iterate the array and swap the elements that doesn't match this arrangements,e.g.,
Read full article from Given an array of integers, s... | CareerCup
No comments:
Post a Comment