|
| 1 | +<h2><a href="https://leetcode.com/problems/partition-array-according-to-given-pivot/">2161. Partition Array According to Given Pivot</a></h2><h3>Medium</h3><hr><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> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>Every element less than <code>pivot</code> appears <strong>before</strong> every element greater than <code>pivot</code>.</li> |
| 5 | + <li>Every element equal to <code>pivot</code> appears <strong>in between</strong> the elements less than and greater than <code>pivot</code>.</li> |
| 6 | + <li>The <strong>relative order</strong> of the elements less than <code>pivot</code> and the elements greater than <code>pivot</code> is maintained. |
| 7 | + <ul> |
| 8 | + <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. If <code>i < j</code> and <strong>both</strong> elements are smaller (<em>or larger</em>) than <code>pivot</code>, then <code>p<sub>i</sub> < p<sub>j</sub></code>.</li> |
| 9 | + </ul> |
| 10 | + </li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>Return <code>nums</code><em> after the rearrangement.</em></p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10 |
| 20 | +<strong>Output:</strong> [9,5,3,10,10,12,14] |
| 21 | +<strong>Explanation:</strong> |
| 22 | +The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. |
| 23 | +The elements 12 and 14 are greater than the pivot so they are on the right side of the array. |
| 24 | +The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> nums = [-3,4,3,2], pivot = 2 |
| 31 | +<strong>Output:</strong> [-3,2,4,3] |
| 32 | +<strong>Explanation:</strong> |
| 33 | +The element -3 is less than the pivot so it is on the left side of the array. |
| 34 | +The elements 4 and 3 are greater than the pivot so they are on the right side of the array. |
| 35 | +The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings. |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 43 | + <li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li> |
| 44 | + <li><code>pivot</code> equals to an element of <code>nums</code>.</li> |
| 45 | +</ul> |
0 commit comments