2161. Partition Array According to Given Pivot LeetCode Solution

In this guide, you will get 2161. Partition Array According to Given Pivot LeetCode Solution with the best time and space complexity. The solution to Partition Array According to Given Pivot problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Partition Array According to Given Pivot solution in C++
  4. Partition Array According to Given Pivot solution in Java
  5. Partition Array According to Given Pivot solution in Python
  6. Additional Resources
2161. Partition Array According to Given Pivot LeetCode Solution image

Problem Statement of Partition Array According to Given Pivot

You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:

Every element less than pivot appears before every element greater than pivot.
Every element equal to pivot appears in between the elements less than and greater than pivot.
The relative order of the elements less than pivot and the elements greater than pivot is maintained.

More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.

Return nums after the rearrangement.

Example 1:

Input: nums = [9,12,5,10,14,3,10], pivot = 10
Output: [9,5,3,10,10,12,14]
Explanation:
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
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.

See also  2897. Apply Operations on Array to Maximize Sum of Squares LeetCode Solution

Example 2:

Input: nums = [-3,4,3,2], pivot = 2
Output: [-3,2,4,3]
Explanation:
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.

Constraints:

1 <= nums.length <= 105
-106 <= nums[i] <= 106
pivot equals to an element of nums.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2161. Partition Array According to Given Pivot LeetCode Solution in C++

class Solution {
 public:
  vector<int> pivotArray(vector<int>& nums, int pivot) {
    vector<int> ans;

    for (const int num : nums)
      if (num < pivot)
        ans.push_back(num);

    for (const int num : nums)
      if (num == pivot)
        ans.push_back(num);

    for (const int num : nums)
      if (num > pivot)
        ans.push_back(num);

    return ans;
  }
};
/* code provided by PROGIEZ */

2161. Partition Array According to Given Pivot LeetCode Solution in Java

class Solution {
  public int[] pivotArray(int[] nums, int pivot) {
    int[] ans = new int[nums.length];
    int i = 0; // ans' index

    for (final int num : nums)
      if (num < pivot)
        ans[i++] = num;

    for (final int num : nums)
      if (num == pivot)
        ans[i++] = num;

    for (final int num : nums)
      if (num > pivot)
        ans[i++] = num;

    return ans;
  }
}
// code provided by PROGIEZ

2161. Partition Array According to Given Pivot LeetCode Solution in Python

class Solution:
  def pivotArray(self, nums: list[int], pivot: int) -> list[int]:
    return ([num for num in nums if num < pivot] +
            [num for num in nums if num == pivot] +
            [num for num in nums if num > pivot])
# code by PROGIEZ

Additional Resources

See also  290. Word Pattern LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.