This problem is very similar to “2-Way Partition”, but now the goal is to partition an input array more carefully.
Given: A positive integer n≤105 and an array A[1..n] of integers from −105 to 105.
Return: An array B[1..n] such that it is a permutation of A and there are indices 1≤q≤r≤n such that
B[i]<A[1] for all 1≤i≤q−1, B[i]=A[1] for all q≤i≤r, and B[i]>A[1] for all r+1≤i≤n.