BackmediumArraysPhonePe

Three-Way Pivot Partitioning Solution

Problem Statement

Given an array of integers nums and an integer pivot, rearrange the elements of nums in-place such that all elements less than pivot appear first, followed by all elements equal to pivot, and finally all elements greater than pivot appear last. The relative order of the partitioned elements should be preserved.

Example 1
Input
[3, 5, 5, 5, 9, 10, 12, 14]
Output
[3, 9, 10, 12, 14, 5, 5, 5]

Explanation: Step-by-step: With input [3, 5, 5, 5, 9, 10, 12, 14], we first partition the array into three parts: elements less than 5, elements equal to 5, and elements greater than 5. The relative order of the partitioned elements is preserved by iterating through the array only once.

Example 2
Input
[1, 2, 4, 4, 7, 8]
Output
[1, 2, 4, 4, 7, 8]

Explanation: Step-by-step: With input [1, 2, 4, 4, 7, 8], we first partition the array into three parts: elements less than 4, elements equal to 4, and elements greater than 4. The relative order of the partitioned elements is preserved by iterating through the array only once.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= pivot <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Three-Way Pivot Partitioning — Problem Statement & Solution Guide

ArraysMediumDutch National Flag
TimeO(N)
|
SpaceO(1)

Problem Description

Given an array of integers nums and an integer pivot, rearrange the elements of nums in-place such that all elements less than pivot appear first, followed by all elements equal to pivot, and finally all elements greater than pivot appear last. The relative order of the partitioned elements should be preserved.

Examples

Example 1

Input

[3, 5, 5, 5, 9, 10, 12, 14]

Output

[3, 9, 10, 12, 14, 5, 5, 5]

Explanation: Step-by-step: With input [3, 5, 5, 5, 9, 10, 12, 14], we first partition the array into three parts: elements less than 5, elements equal to 5, and elements greater than 5. The relative order of the partitioned elements is preserved by iterating through the array only once.

Example 2

Input

[1, 2, 4, 4, 7, 8]

Output

[1, 2, 4, 4, 7, 8]

Explanation: Step-by-step: With input [1, 2, 4, 4, 7, 8], we first partition the array into three parts: elements less than 4, elements equal to 4, and elements greater than 4. The relative order of the partitioned elements is preserved by iterating through the array only once.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= pivot <= 10^9

Optimal Approach & Strategy

We use Dijkstra's Dutch National Flag algorithm with three pointers: low, mid, and high. The low pointer tracks the boundary of elements less than the pivot, mid scans the current element, and high tracks the boundary of elements greater than the pivot. By swapping elements relative to these boundaries in a single pass, we achieve O(N) time complexity and O(1) auxiliary space.

Brute Force Approach

Create three auxiliary lists to separate elements that are less than, equal to, and greater than the pivot. Iterate through the input array and append each element to its respective list. Finally, copy the elements from the three lists back into the original array in order. This requires O(N) extra space.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function threeWayPivotPartitioning(nums, pivot) {
  let low = 0;
  let mid = 0;
  let high = nums.length - 1;
  while (mid <= high) {
    if (nums[mid] < pivot) {
      let temp = nums[low];
      nums[low] = nums[mid];
      nums[mid] = temp;
      low++;
      mid++;
    } else if (nums[mid] === pivot) {
      mid++;
    } else {
      let temp = nums[high];
      nums[high] = nums[mid];
      nums[mid] = temp;
      high--;
    }
  }
  return nums;
}

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.