Three Way Partitioning

Problem Link: https://practice.geeksforgeeks.org/problems/three-way-partitioning/1

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 
	    n=len(arr)
	    i=0
	    j=0
	    k=n-1
	    while(j<=k):
	        if(arr[j]<a):
	            arr[i],arr[j]=arr[j],arr[i]
	            i+=1
	            j+=1
	        elif(arr[j]>=a and arr[j]<=b):
	            j+=1
	        else:
	            arr[j],arr[k]=arr[k],arr[j]
	            k-=1
        return arr

Last updated