From ca07c0f4c7feae9fc894186ed68c364109a920bc Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 3 Mar 2025 15:49:10 +0530 Subject: [PATCH] Create 2161. Partition Array According to Given Pivot --- .... Partition Array According to Given Pivot | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2161. Partition Array According to Given Pivot diff --git a/2161. Partition Array According to Given Pivot b/2161. Partition Array According to Given Pivot new file mode 100644 index 0000000..5231d66 --- /dev/null +++ b/2161. Partition Array According to Given Pivot @@ -0,0 +1,36 @@ +class Solution { +public: + vector pivotArray(vector& nums, int pivot) { + int n = nums.size(); + vector ans(n); + int lesser = 0, greater = 0, count = 0; + int lesserIndex = 0, pivotIndex = 0, greaterIndex = 0; + + for (int i = 0; i < n; i++) { + if (nums[i] < pivot) { + lesser++; + } + else if (nums[i] == pivot) { + count++; + } + else { + greater++; + } + } + pivotIndex = lesser; + greaterIndex = lesser + count; + + for (int index = 0; index < n; index++) { + if (lesserIndex < lesser && nums[index] < pivot) { + ans[lesserIndex++] = nums[index]; + } + else if (pivotIndex < (lesser + count) && nums[index] == pivot) { + ans[pivotIndex++] = nums[index]; + } + else if (greaterIndex < n && nums[index] > pivot) { + ans[greaterIndex++] = nums[index]; + } + } + return ans; + } +};