Skip to main content
LeetCode 280, Medium. Topics: Greedy, Array, Sorting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

Given an integer array nums, reorder it such that nums[0] <= nums[1] >= nums[2] <= nums[3].... You may assume the input array always has a valid answer. Follow up: Could you solve the problem in O(n) time complexity?

Examples

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • 0 <= nums[i] <= 10^4
  • It is guaranteed that there will be an answer for the given input nums.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026