| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.PartitionArrayAccordingToGivenPivot; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class PartitionArrayAccordingToGivenPivotTwoPointers : IPartitionArrayAccordingToGivenPivot |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="pivot"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[] PivotArray(int[] nums, int pivot) |
| 2 | 25 | | { |
| 2 | 26 | | var result = new int[nums.Length]; |
| 2 | 27 | | var leftIndex = 0; |
| 2 | 28 | | var rightIndex = nums.Length - 1; |
| | 29 | |
|
| 39 | 30 | | for (int i = 0, j = nums.Length - 1; i < nums.Length; i++, j--) |
| 11 | 31 | | { |
| 11 | 32 | | if (nums[i] < pivot) |
| 4 | 33 | | { |
| 4 | 34 | | result[leftIndex] = nums[i]; |
| 4 | 35 | | leftIndex++; |
| 4 | 36 | | } |
| | 37 | |
|
| 11 | 38 | | if (nums[j] > pivot) |
| 4 | 39 | | { |
| 4 | 40 | | result[rightIndex] = nums[j]; |
| 4 | 41 | | rightIndex--; |
| 4 | 42 | | } |
| 11 | 43 | | } |
| | 44 | |
|
| 5 | 45 | | while (leftIndex <= rightIndex) |
| 3 | 46 | | { |
| 3 | 47 | | result[leftIndex] = pivot; |
| 3 | 48 | | leftIndex++; |
| 3 | 49 | | } |
| | 50 | |
|
| 2 | 51 | | return result; |
| 2 | 52 | | } |
| | 53 | | } |