Maximising cardinality of a set given the mean. Given a set of distinct, positive integers of mean $x$ and largest element $y$ how can we maximise the cardinality of the set?

For a corresponding set of $n$ elements, there must be a partition of $nx-y$ into $n-1$ unique addends. If it exists, you've found the maximum cardinality - $n$. Finding the maximum value requires three cases:


*

*$y=x$, the cardinality is $1$ (set $\{x\}$)

*$y < 2x$, the cardinality is $\lfloor \frac{1}{2} (\sqrt{4 x^2-8 x y+4 x+4 y^2+4 y+1}-2 x+2 y-1) \rfloor$

*$y \geq 2x$, cardinality is$\lfloor \frac{1}{2} (\sqrt{4 x^2+4 x-8 y+1}+2 x+1)) \rfloor$


This gives you $5$ for $x=3, y=5$ or $8$ for $x=5, y=11$ (the set for the latter is $\{1, 2, 3, 4, 5, 6, 8, 11\}$). The sets themselves can be found trivially.
