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
numsin non-increasing order. - Sort the values at even indices of
numsin non-decreasing order.
Return the array formed after rearranging the values of nums.
Example 1
Input
nums = [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
Input
nums = [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