java.lang.Object
g2101_2200.s2161_partition_array_according_to_given_pivot.Solution

public class Solution extends Object
2161 - Partition Array According to Given Pivot.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>pivot</code>. Rearrange <code>nums</code> such that the following conditions are satisfied:</p> <ul> <li>Every element less than <code>pivot</code> appears <strong>before</strong> every element greater than <code>pivot</code>.</li> <li>Every element equal to <code>pivot</code> appears <strong>in between</strong> the elements less than and greater than <code>pivot</code>.</li> <li>The <strong>relative order</strong> of the elements less than <code>pivot</code> and the elements greater than <code>pivot</code> is maintained. <ul> <li>More formally, consider every <code>p<sub>i</sub></code>, <code>p<sub>j</sub></code> where <code>p<sub>i</sub></code> is the new position of the <code>i<sup>th</sup></code> element and <code>p<sub>j</sub></code> is the new position of the <code>j<sup>th</sup></code> element. For elements less than <code>pivot</code>, if <code>i < j</code> and <code>nums[i] < pivot</code> and <code>nums[j] < pivot</code>, then <code>p<sub>i</sub> < p<sub>j</sub></code>. Similarly for elements greater than <code>pivot</code>, if <code>i < j</code> and <code>nums[i] > pivot</code> and <code>nums[j] > pivot</code>, then <code>p<sub>i</sub> < p<sub>j</sub></code>.</li> </ul> </li> </ul> <p>Return <code>nums</code> <em>after the rearrangement.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10</p> <p><strong>Output:</strong> [9,5,3,10,10,12,14]</p> <p><strong>Explanation:</strong> The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.</p> <p>The elements 12 and 14 are greater than the pivot so they are on the right side of the array.</p> <p>The relative ordering of the elements less than and greater than pivot is also maintained.</p> <p>[9, 5, 3] and [12, 14] are the respective orderings.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [-3,4,3,2], pivot = 2</p> <p><strong>Output:</strong> [-3,2,4,3]</p> <p><strong>Explanation:</strong> The element -3 is less than the pivot so it is on the left side of the array.</p> <p>elements 4 and 3 are greater than the pivot so they are on the right side of the array.</p> <p>The relative ordering of the elements less than and greater than pivot is also maintained.</p> <p>[-3] and [4, 3] are the respective orderings.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li> <li><code>pivot</code> equals to an element of <code>nums</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • pivotArray

      public int[] pivotArray(int[] nums, int pivot)