Three way partitioning | Practice | GeeksforGeeks

Given an array of size n and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified array.

Example 1:
Input: 
n = 5
A[] = {1, 2, 3, 3, 4}
[a, b] = [1, 2]
Output: 1
Explanation: One possible arrangement is:
{1, 2, 3, 3, 4}. If you return a valid
arrangement, output will be 1.



Example 2:
Input: 
n = 3 
A[] = {1, 2, 3}
[a, b] = [1, 3]
Output: 1
Explanation: One possible arrangement 
is: {1, 2, 3}. If you return a valid
arrangement, output will be 1.



Your Task: 
You dont need to read input or print anything. The task is to complete the function threeWayPartition() which takes the array[], a and b as input parameters and modifies the array in-place according to the given conditions.
Note: The generated output is 1 if you modify the given array successfully.


Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)


Constraints:
1 <= n <= 106
1 <= A[i] <= 106
0
saicharank0183 days ago
class Solution:   #Function to partition the array around the range such    #that array is divided into three parts.def threeWayPartition(self, arr, a, b):    # code here     l=0;r=len(arr)-1;i=0    #l points to the starting point of ele>=a    #r points to last ele of ele<b    while i<=r:        if arr[i]<a:            arr[l],arr[i]=arr[i],arr[l]            i+=1;l+=1        elif arr[i]>b:            arr[r],arr[i]=arr[i],arr[r]            r-=1        else:            i+=1
O(N) time and O(1) Space
0
codewithshoaib194 days ago
public void threeWayPartition(int array[], int a, int b)   {       // code here       Arrays.sort(array);       int j=0;       for(int i=0;i<array.length;i++){           // Moving all elements lower than a to start           if(array[i]<a){               array[j]=array[i];           j++;           }       }       for(int i=0;i<array.length;i++){           // Moving elements in range a and b to middle           if(array[i]>=a && array[i]<=b){               array[j]=array[i];               j++;           }       }       for(int i=0;i<array.length;i++){           // Moving all elements greater than b to end           if(array[i]>b){               array[j]=array[i];           j++;           }       }       // System.out.println(j);       // System.out.println(Arrays.toString(array));   }
0
atif836144 days ago
JAVA SOLUTION
class Solution{
   
    public void threeWayPartition(int array[], int a, int b)
    {
    if(array.length==0){
    return;
    }
    int start=0;
    int end=array.length-1;
    for(int i=0;i<=end;){
    if(array[i]<a){
    int temp=array[start];
    array[start]=array[i];
    array[i]=temp;
    start++;
    i++;
    }
    else if(array[i]>b){
    int temp=array[end];
    array[end]=array[i];
    array[i]=temp;
    end--;
    }
    else
    i++;
    }
    }
    }
-1
6780apoorvyadav1 week ago
class Solution{   
public:
    void threeWayPartition(vector<int>& array,int a, int b)
    {
        // code here 
        sort(array.begin(), array.end());
    }
};
0
sailendrachettri1 week ago
C++ solution
 void threeWayPartition(vector<int>& array,int a, int b)
    {
        // code here 
        sort(array.begin(), array.end());
        
    }
0
shubham211019972 weeks ago
//dutch national flag Algorithm
void threeWayPartition(vector<int>& arr,int a, int b)   {       int low=0,mid=0,hi=arr.size()-1;      while(mid<=hi){       if(arr[mid]<a){                      swap(arr[low],arr[mid]);           mid++;           low++;       }       else if(arr[mid]>=a && arr[mid]<=b){           mid++;       }       else{          swap(arr[mid],arr[hi]);           hi--;       }        }   }
+2
pratyushhota19992 weeks ago
Why this is not a twisted definition of sorting an array ?
0
vkbhagat993 weeks ago
c++++++++++++++++++++;
 void threeWayPartition(vector<int>& array,int a, int b)   {       int l=0;   int r=array.size()-1;   for(int i=0;i<=r;i++)   {              if(array[i]<a)       {           swap(array[i],array[l]);           l++;       }       else if(array[i]>b)       {           swap(array[i],array[r]);           // temp =arr[i];           // arr[i]=arr[r];           // arr[r]=temp;           r--;           i--;                  }   }       // code here    }
0
nshreya081 month ago
public void threeWayPartition(int array[], int a, int b)   {       // code here        Arrays.sort(array);   }
+6
apurvkumarak1 month ago
C++ Solution 
    void threeWayPartition(vector<int>& arr,int a, int b){
        int l=-1,h=arr.size();
        for(int i=0;i<arr.size();i++){
            if(arr[i] < a){
                l++;
                swap(arr[i],arr[l]);
            }
        }
        for(int i=arr.size()-1;i>=0;i--){
            if(arr[i] > b){
                h--;
                swap(arr[i],arr[h]);
            }
        }
        
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.