3 pointer Approach Space O(1) Time O(n)


#1

void Solution::sortColors(vector &A) {
int n=A.size();
int l=0,m=0,h=n-1;
while(m<=h)
{
if(A[m]==0)
swap(A[l++],A[m++]);
else if(A[m]==1)
m++;
else
swap(A[m],A[h–]);

}

}


#2

Too good, How did u comeup with this technique