FastPrepFastPrep
Problem Brief

Meandering Array

OA

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 Description

Complete the function meanderingArray in the editor below.

meanderingArray has the following parameters:

  1. int unsorted[n]: the unsorted array

Returns

int[n]: the array sorted in meandering order

1Example 1

Input
unsorted = [-1, 1, 2, 3, -5]
Output
[3, -5, 2, -1, 1]
Explanation
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].

Constraints

Limits and guarantees your solution can rely on.

  • 2≤n≤10^5
  • -10^6 ≤ unsorted[i] ≤ 10^6
  • The unsorted array may contain duplicate elements.
  • public int[] meanderingArray(int[] unsorted) {
      // write your code here
    }
    
    Input

    unsorted

    [-1, 1, 2, 3, -5]

    Output

    [3, -5, 2, -1, 1]

    Sign in to submit your solution.