Problem · Array

Meandering Array

Learn this problem
EasyIBM logoIBMOA
See IBM hiring insights

Problem 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:

  1. 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].

Constraints

  • 2≤n≤10^5
  • -10^6 ≤ unsorted[i] ≤ 10^6
  • The unsorted array may contain duplicate elements.
  • More IBM problems

    drafts saved locally
    public int[] meanderingArray(int[] unsorted) {
      // write your code here
    }
    
    unsorted[-1, 1, 2, 3, -5]
    expected[3, -5, 2, -1, 1]
    checking account