We first simplify the definition of beauty.
For a subarray b, a value k is valid iff k≤min(b). After subtracting such a k, the resulting array contains 0 (take k=min(b)), and maximizing MEX is clearly best when we make the minimum equal to 0.
So the beauty of a subarray equals:
the largest m such that the subarray contains all values min(b),min(b)+1,…,min(b)+m−1.
Equivalently, if the minimum is x, then beauty is the maximum y such that all numbers x,x+1,…,x+y−1 appear inside the subarray.