2164. Sort Even and Odd Indices Independently Easy
1/**
2 * [2164] Sort Even and Odd Indices Independently
3 *
4 * You are given a 0-indexed integer array nums. Rearrange the values of nums according to the following rules:
5 * <ol>
6 * Sort the values at odd indices of nums in non-increasing order.
7 *
8 * For example, if nums = [4,<u>1</u>,2,<u>3</u>] before this step, it becomes [4,<u>3</u>,2,<u>1</u>] after. The values at odd indices 1 and 3 are sorted in non-increasing order.
9 *
10 *
11 * Sort the values at even indices of nums in non-decreasing order.
12 *
13 * For example, if nums = [<u>4</u>,1,<u>2</u>,3] before this step, it becomes [<u>2</u>,1,<u>4</u>,3] after. The values at even indices 0 and 2 are sorted in non-decreasing order.
14 *
15 *
16 * </ol>
17 * Return the array formed after rearranging the values of nums.
18 *
19 * Example 1:
20 *
21 * Input: nums = [4,1,2,3]
22 * Output: [2,3,4,1]
23 * Explanation:
24 * First, we sort the values present at odd indices (1 and 3) in non-increasing order.
25 * So, nums changes from [4,<u>1</u>,2,<u>3</u>] to [4,<u>3</u>,2,<u>1</u>].
26 * Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
27 * So, nums changes from [<u>4</u>,1,<u>2</u>,3] to [<u>2</u>,3,<u>4</u>,1].
28 * Thus, the array formed after rearranging the values is [2,3,4,1].
29 *
30 * Example 2:
31 *
32 * Input: nums = [2,1]
33 * Output: [2,1]
34 * Explanation:
35 * Since there is exactly one odd index and one even index, no rearrangement of values takes place.
36 * The resultant array formed is [2,1], which is the same as the initial array.
37 *
38 *
39 * Constraints:
40 *
41 * 1 <= nums.length <= 100
42 * 1 <= nums[i] <= 100
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/sort-even-and-odd-indices-independently/
48// discuss: https://leetcode.com/problems/sort-even-and-odd-indices-independently/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn sort_even_odd(nums: Vec<i32>) -> Vec<i32> {
54 vec![]
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_2164() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.