The final common value cannot exceed , so the maximum element needs at least decrements. Decreasing all elements above each time attains this bound. The answer is the range, in time and auxiliary space. Sorting is a naive solution that also passes. The answer is at most , within signed 32-bit; the implementations use 64-bit.
Solution written by GPT6