Skip to content

Instantly share code, notes, and snippets.

@abhiagx
Last active April 14, 2016 16:01
Show Gist options
  • Save abhiagx/2d6c2a74ecca2a8014ee9acfad15d2ca to your computer and use it in GitHub Desktop.
Save abhiagx/2d6c2a74ecca2a8014ee9acfad15d2ca 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..... Example Given [1, 2, 3, 4] One possible answer : [2, 1, 4, 3] Another possible answer : [4, 1, 3, 2]. In case of multiple possible answers return the one thats lexicograph…
vector<int> Solution::wave(vector<int> &A) {
sort(A.begin(), A.end());
int s = A.size();
int i = 0;
while (i<=s-2)
{
swap(A[i], A[i+1]);
i += 2;
}
return A;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment