/**
* Problem: LeetCode 2161 - Partition Array According to Given Pivot
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int[] pivotArray(int[] nums, int pivot) {
int n = nums.length;
int[] result = new int[n];
int index = 0;
// Elements less than pivot
for (int num : nums) {
if (num < pivot) {
result[index++] = num;
}
}
// Elements equal to pivot
for (int num : nums) {
if (num == pivot) {
result[index++] = num;
}
}
// Elements greater than pivot
for (int num : nums) {
if (num > pivot) {
result[index++] = num;
}
}
return result;
}
}
"""
Problem: LeetCode 2161 - Partition Array According to Given Pivot
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
n = len(nums)
result = [0] * n
index = 0
# Elements less than pivot
for num in nums:
if num < pivot:
result[index] = num
index += 1
# Elements equal to pivot
for num in nums:
if num == pivot:
result[index] = num
index += 1
# Elements greater than pivot
for num in nums:
if num > pivot:
result[index] = num
index += 1
return result
/**
* Problem: LeetCode 2161 - Partition Array According to Given Pivot
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
/**
* Problem: LeetCode 2161 - Partition Array According to Given Pivot
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
vector<int> pivotArray(vector<int>& nums, int pivot) {
int n = nums.size();
vector<int> result(n);
int index = 0;
// Elements less than pivot
for (int num : nums) {
if (num < pivot) {
result[index++] = num;
}
}
// Elements equal to pivot
for (int num : nums) {
if (num == pivot) {
result[index++] = num;
}
}
// Elements greater than pivot
for (int num : nums) {
if (num > pivot) {
result[index++] = num;
}
}
return result;
}
};
/**
* Problem: LeetCode 2161 - Partition Array According to Given Pivot
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var pivotArray = function(nums, pivot) {
const n = nums.length;
const result = new Array(n);
let index = 0;
// Elements less than pivot
for (let i = 0; i < n; i++) {
if (nums[i] < pivot) {
result[index++] = nums[i];
}
}
// Elements equal to pivot
for (let i = 0; i < n; i++) {
if (nums[i] === pivot) {
result[index++] = nums[i];
}
}
// Elements greater than pivot
for (let i = 0; i < n; i++) {
if (nums[i] > pivot) {
result[index++] = nums[i];
}
}
return result;
};