Created
August 28, 2015 14:47
-
-
Save viveksyngh/36f3bfb753e91310d369 to your computer and use it in GitHub Desktop.
Given an array of integers, sort the array into a wave like array and return it, In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5.....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param A : A List of integers | |
# @return List of integers | |
def wave(A): | |
A.sort() | |
i = 0 | |
while i < len(A) : | |
if i + 1 < len(A) : | |
A[i], A[i+1] = A[i+1], A[i] | |
i = i + 2 | |
else : | |
i = i + 1 | |
return A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment