java.lang.Object
g2101_2200.s2164_sort_even_and_odd_indices_independently.Solution

public class Solution extends Object
2164 - Sort Even and Odd Indices Independently.<p>Easy</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. Rearrange the values of <code>nums</code> according to the following rules:</p> <ol> <li>Sort the values at <strong>odd indices</strong> of <code>nums</code> in <strong>non-increasing</strong> order. <ul> <li>For example, if <code>nums = [4, <strong>1</strong> ,2, <strong>3</strong> ]</code> before this step, it becomes <code>[4, <strong>3</strong> ,2, <strong>1</strong> ]</code> after. The values at odd indices <code>1</code> and <code>3</code> are sorted in non-increasing order.</li> </ul> </li> <li>Sort the values at <strong>even indices</strong> of <code>nums</code> in <strong>non-decreasing</strong> order. <ul> <li>For example, if <code>nums = [<strong>4</strong> ,1, <strong>2</strong> ,3]</code> before this step, it becomes <code>[<strong>2</strong> ,1, <strong>4</strong> ,3]</code> after. The values at even indices <code>0</code> and <code>2</code> are sorted in non-decreasing order.</li> </ul> </li> </ol> <p>Return <em>the array formed after rearranging the values of</em> <code>nums</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [4,1,2,3]</p> <p><strong>Output:</strong> [2,3,4,1]</p> <p><strong>Explanation:</strong></p> <p>First, we sort the values present at odd indices (1 and 3) in non-increasing order.</p> <p>So, nums changes from [4, <strong>1</strong> ,2, <strong>3</strong> ] to [4, <strong>3</strong> ,2, <strong>1</strong> ].</p> <p>Next, we sort the values present at even indices (0 and 2) in non-decreasing order. So, nums changes from [<strong>4</strong> ,1, <strong>2</strong> ,3] to [<strong>2</strong> ,3, <strong>4</strong> ,1].</p> <p>Thus, the array formed after rearranging the values is [2,3,4,1].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,1]</p> <p><strong>Output:</strong> [2,1]</p> <p><strong>Explanation:</strong> Since there is exactly one odd index and one even index, no rearrangement of values takes place.</p> <p>The resultant array formed is [2,1], which is the same as the initial array.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 100</code></li> <li><code>1 <= nums[i] <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sortEvenOdd

      public int[] sortEvenOdd(int[] nums)