You are given a 0-indexed array of positive integers nums and a positive integer limit. In one operation, you can choose any two indices i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit. Return the lexicographically smallest array that can be obtained by performing the operation any number of times. An array a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.
Example 1
Input: nums = [1,5,3,9,8], limit = 2
Output: [1,3,5,8,9]
Explanation: Values {1,3,5} form one swappable group (positions 0,1,2) and {8,9} another (positions 3,4). Smallest values go to smallest positions.
Example 2
Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]
Explanation: Sorted, the values are 1, 1, 2, 6, 7, 18. Values within 3 of each other chain into groups: {1,1,2}, {6,7} and {18}. The group {1,1,2} sits at indices 0, 4, 5 and gets 1, 1, 2 in order. The group {6,7} sits at indices 1, 2 and gets 6, 7. 18 stays at index 3.
Example 3
Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
Explanation: Only 7 and 10 are close enough to swap (difference 3), and 7 already comes first. Every other value is more than 3 away from the rest, so the array is already the smallest possible.
If we sort the values, any run of adjacent values whose consecutive gaps are all <= limit are mutually swappable (swaps chain transitively). Within each such group the positions can be reordered freely, so place the smallest values at the smallest positions.
Run checks all cases above. Submit evaluates all test cases.