Sort Even and Odd Indices Independently

You are given a 0-indexed integer array nums. Rearrange the values of nums according to the following rules:

  • Sort the values at odd indices of nums in non-increasing order.
  • Sort the values at even indices of nums in non-decreasing order.

Return the array formed after rearranging the values of nums.

Example 1
Inputnums = [4,1,2,3]
Output[2,3,4,1]
Odd-indexed values 1 and 3 are sorted in non-increasing order, then even-indexed values 4 and 2 are sorted in non-decreasing order, producing [2, 3, 4, 1].
Example 2
Inputnums = [2,1]
Output[2,1]
Since there is exactly one odd index and one even index, no rearrangement of values takes place.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate