Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2164.html
2164. Sort Even and Odd Indices Independently (Easy)
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.- For example, if
nums = [4,1,2,3]
before this step, it becomes[4,3,2,1]
after. The values at odd indices1
and3
are sorted in non-increasing order.
- For example, if
- Sort the values at even indices of
nums
in non-decreasing order.- For example, if
nums = [4,1,2,3]
before this step, it becomes[2,1,4,3]
after. The values at even indices0
and2
are sorted in non-decreasing order.
- For example, if
Return the array formed after rearranging the values of nums
.
Example 1:
Input: nums = [4,1,2,3] Output: [2,3,4,1] Explanation: First, we sort the values present at odd indices (1 and 3) in non-increasing order. So, nums changes from [4,1,2,3] to [4,3,2,1]. Next, we sort the values present at even indices (0 and 2) in non-decreasing order. So, nums changes from [4,1,2,3] to [2,3,4,1]. Thus, the array formed after rearranging the values is [2,3,4,1].
Example 2:
Input: nums = [2,1] Output: [2,1] Explanation: Since there is exactly one odd index and one even index, no rearrangement of values takes place. The resultant array formed is [2,1], which is the same as the initial array.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Similar Questions:
Solution 1.
-
class Solution { public int[] sortEvenOdd(int[] nums) { int n = nums.length; int[] a = new int[(n + 1) >> 1]; int[] b = new int[n >> 1]; for (int i = 0, j = 0; j<n> > 1; i += 2, ++j) { a[j] = nums[i]; b[j] = nums[i + 1]; } if (n % 2 == 1) { a[a.length - 1] = nums[n - 1]; } Arrays.sort(a); Arrays.sort(b); int[] ans = new int[n]; for (int i = 0, j = 0; j < a.length; i += 2, ++j) { ans[i] = a[j]; } for (int i = 1, j = b.length - 1; j >= 0; i += 2, --j) { ans[i] = b[j]; } return ans; } }
-
class Solution { public: vector<int> sortEvenOdd(vector<int>& nums) { int n = nums.size(); vector<int> a; vector<int> b; for (int i = 0; i < n; ++i) { if (i % 2 == 0) a.push_back(nums[i]); else b.push_back(nums[i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end(), greater<int>()); vector<int> ans(n); for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j]; for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j]; return ans; } };
-
class Solution: def sortEvenOdd(self, nums: List[int]) -> List[int]: a = sorted(nums[::2]) b = sorted(nums[1::2], reverse=True) nums[::2] = a nums[1::2] = b return nums
-
func sortEvenOdd(nums []int) []int { n := len(nums) var a []int var b []int for i, v := range nums { if i%2 == 0 { a = append(a, v) } else { b = append(b, v) } } ans := make([]int, n) sort.Ints(a) sort.Slice(b, func(i, j int) bool { return b[i] > b[j] }) for i, j := 0, 0; j < len(a); i, j = i+2, j+1 { ans[i] = a[j] } for i, j := 1, 0; j < len(b); i, j = i+2, j+1 { ans[i] = b[j] } return ans }