Free written solution

Make Lexicographically Smallest Array by Swapping Elements (LeetCode 2948): Solution and Explanation

You may swap two values whenever they differ by at most limit. Return the smallest array in dictionary order that any sequence of such swaps can produce. The trick is that swappable values form groups that are contiguous runs once the values are sorted.

Read the intuition, approach, code and complexity →