Wave Array

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]

NOTE : If there are multiple answers possible, return the one thats lexicographically smallest. So, in example case, you will return [2, 1, 4, 3] NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

Solution: Using Sorting

  1. Sort the array
  2. Swap element in pairs
vector<int> wave(vector<int> Vec) {
    sort(Vec.begin(), Vec.end());
    int N = Vec.size();
    for(int i = 0; i < N - 1; i += 2) {
        swap(Vec[i], Vec[i + 1]);
    }
    return Vec;
}

results matching ""

    No results matching ""