Problem · Array
Meandering Array
Learn this problemProblem statement
An array of integers is defined as being in meandering order when the first two elements are the respective largest and smallest elements in the array and the subsequent elements alternate between its next largest and next smallest elements. In other words, the elements are in order of [first_largest, first_smallest, second_largest, second_smallest, ...].
Function
meanderingArray(unsorted: int[]) → int[]
Complete the function meanderingArray in the editor below.
meanderingArray has the following parameters:
int unsorted[n]: the unsorted array
Returns
int[n]: the array sorted in meandering order
Examples
Example 1
unsorted = [-1, 1, 2, 3, -5]return = [3, -5, 2, -1, 1]The array [-1, 1, 2, 3, -5] sorted normally is [-5, -1, 1, 2, 3]. Sorted in meandering order, it becomes [3, -5, 2, -1, 1].