Ctrl K

Two Orders in One Array

Easy
You are given an array values, with positions counted from zero. The numbers at even positions (0, 2, 4, …) and at odd positions (1, 3, 5, …) are handled separately: - sort the numbers at even positions in non-decreasing order; - sort the numbers at odd positions in non-increasing order. A number never moves from an even position to an odd one or the other way round. Return the resulting array.
Example 1
values=[9, 2, 4, 6, 1, 3] → [1, 6, 4, 3, 9, 2]

The even positions hold 9, 4, 1, which ascending is 1, 4, 9. The odd positions hold 2, 6, 3, which descending is 6, 3, 2. Together: [1, 6, 4, 3, 9, 2].

Example 2
values=[5, 5, 2] → [2, 5, 5]

On the even positions 5 and 2 swap. The only odd position keeps its number.

Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge Into the Spare Tail EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyMake It to Every Meeting EasyThe Lost Ticket Easy
loading Python…
Input
values=[9, 2, 4, 6, 1, 3]
Expected
[1, 6, 4, 3, 9, 2]

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.