How to find the maximal set of elements $S$ of an array such that every element in $S$ is greater than or equal to the cardinality of $S$?
Sort T. Then take elements while T[i] &gt;= i+1.

For example sorted(T)=[6,4,3,3,1,1]. Then, T[0] = 6 &gt; 1, T[1] = 4 &gt; 2, T[2] = 3 &lt;= 3 and finally, T[3] = 3 &lt; 4 so we have S = [T[0], T[1], T[2]].