Three way partitioning of an Array without changing the relative ordering
Given an array and a range [lowVal, highVal], partition the array around the range such that the array is divided into three parts.
- All elements smaller than lowVal come first.
- All elements in range lowVal to highVal come next.
- All elements greater than highVVal appear in the end.
- The relative ordering of numbers shouldn’t be changed.
Examples:
Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}, lowVal = 14, highVal = 20
Output: arr[] = { 1 5 4 2 3 1 14 20 20 54 87 98 32 }
Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}, lowVal = 20, highVal = 20
Output: arr[] = { 1 14 5 4 2 3 1 20 20 54 87 98 32 }
Approach: This approach is based on using extra data structures to store the elements lesser than lowVal, in between lowVal and highVal, and elements greater than highVal. We will be using 3 queue for maintaining the original order of elements.
- Traverse the array one by one
- Insert the array elements into the respective queue one by one
- At the end,
- Pop out all the elements in queue 1 with elements lesser than lowVal
- Then Pop out all the elements in queue 2 with elements in between lowVal and highVal
- Then Pop out all the elements in queue 3 with elements greater than highVal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > pivotArray(vector< int >& nums, int lowVal,
int highVal)
{
queue< int > before, same, after;
for ( int i = 0; i < nums.size(); i++) {
if (nums[i] < lowVal)
before.push(nums[i]);
else if (nums[i] > highVal)
after.push(nums[i]);
else
same.push(nums[i]);
}
int k = 0;
while (before.size() > 0) {
nums[k++] = before.front();
before.pop();
}
while (same.size() > 0) {
nums[k++] = same.front();
same.pop();
}
while (after.size() > 0) {
nums[k++] = after.front();
after.pop();
}
return nums;
}
int main()
{
vector< int > arr
= { 1, 14, 5, 20, 4, 2, 54,
20, 87, 98, 3, 1, 32 };
int lowVal = 20, highVal = 20;
pivotArray(arr, lowVal, highVal);
for ( int i = 0; i < arr.size(); i++) {
cout << arr[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int [] pivotArray( int [] nums, int lowVal,
int highVal)
{
Queue<Integer> before = new LinkedList<>();
Queue<Integer> same = new LinkedList<>();
Queue<Integer> after = new LinkedList<>();
for ( int i = 0 ; i < nums.length; i++) {
if (nums[i] < lowVal)
before.add(nums[i]);
else if (nums[i] > highVal)
after.add(nums[i]);
else
same.add(nums[i]);
}
int k = 0 ;
while (before.size() > 0 ) {
nums[k++] = before.poll();
}
while (same.size() > 0 ) {
nums[k++] = same.poll();
}
while (after.size() > 0 ) {
nums[k++] = after.poll();
}
return nums;
}
public static void main(String[] args)
{
int arr[] = new int [] { 1 , 14 , 5 , 20 , 4 , 2 , 54 ,
20 , 87 , 98 , 3 , 1 , 32 };
int lowVal = 20 , highVal = 20 ;
pivotArray(arr, lowVal, highVal);
for ( int i = 0 ; i < arr.length; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
def pivotArray(nums, lowVal,
highVal):
before = []
same = []
after = []
for i in range ( len (nums)):
if (nums[i] < lowVal):
before.append(nums[i])
elif (nums[i] > highVal):
after.append(nums[i])
else :
same.append(nums[i])
k = 0
while ( len (before) > 0 ):
nums[k] = before[ 0 ]
k + = 1
before.pop( 0 )
while ( len (same) > 0 ):
nums[k] = same[ 0 ]
same.pop( 0 )
k + = 1
while ( len (after) > 0 ):
nums[k] = after[ 0 ]
k + = 1
after.pop( 0 )
return nums
if __name__ = = "__main__" :
arr = [ 1 , 14 , 5 , 20 , 4 , 2 , 54 ,
20 , 87 , 98 , 3 , 1 , 32 ]
lowVal = 20
highVal = 20
pivotArray(arr, lowVal, highVal)
for i in range ( len (arr)):
print (arr[i], end = " " )
|
C#
using System;
using System.Collections;
public class GFG{
static int [] pivotArray( int [] nums, int lowVal,
int highVal)
{
Queue before = new Queue();
Queue same = new Queue();
Queue after = new Queue();
for ( int i = 0; i < nums.Length; i++) {
if (nums[i] < lowVal)
before.Enqueue(nums[i]);
else if (nums[i] > highVal)
after.Enqueue(nums[i]);
else
same.Enqueue(nums[i]);
}
int k = 0;
while (before.Count > 0) {
nums[k++] = ( int )before.Peek();
before.Dequeue();
}
while (same.Count > 0) {
nums[k++] = ( int )same.Peek();
same.Dequeue();
}
while (after.Count > 0) {
nums[k++] = ( int )after.Peek();
after.Dequeue();
}
return nums;
}
static public void Main (){
int [ ] arr
= { 1, 14, 5, 20, 4, 2, 54,
20, 87, 98, 3, 1, 32 };
int lowVal = 20, highVal = 20;
pivotArray(arr, lowVal, highVal);
for ( int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
<script>
const pivotArray = (nums, lowVal, highVal) => {
let before = [], same = [], after = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] < lowVal)
before.push(nums[i]);
else if (nums[i] > highVal)
after.push(nums[i]);
else
same.push(nums[i]);
}
let k = 0;
while (before.length > 0) {
nums[k++] = before[0];
before.shift();
}
while (same.length > 0) {
nums[k++] = same[0];
same.shift();
}
while (after.length > 0) {
nums[k++] = after[0];
after.shift();
}
return nums;
}
let arr = [1, 14, 5, 20, 4, 2, 54,
20, 87, 98, 3, 1, 32];
let lowVal = 20, highVal = 20;
pivotArray(arr, lowVal, highVal);
for (let i = 0; i < arr.length; i++) {
document.write(`${arr[i]} `);
}
</script>
|
Output
1 14 5 4 2 3 1 20 20 54 87 98 32
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(N)
Last Updated :
12 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...