Sorted Distances From a Point
nums is sorted in non-decreasing order and c is a point on the same number line. The distance of an item x from c is abs(x - c). Return the distances of all the items as a list in non-decreasing order.
Because nums is sorted, the item farthest from c is always at one of the two ends, so two indices walking inward from both ends can fill the answer from its largest value down to its smallest without sorting again. An empty list gives an empty list.
Example 1
nums [-4,-1,0,3,10]
c 0
returns [0,1,3,4,10]
The distances from 0 are 4, 1, 0, 3, 10; sorted they are 0, 1, 3, 4, 10.
Example 2
nums [1,2,3,7]
c 4
returns [1,2,3,3]
The distances from 4 are 3, 2, 1, 3; sorted they are 1, 2, 3, 3.
Constraints
0 <= len(nums) <= 100-1000 <= nums[i] <= 1000-1000 <= c <= 1000numsis sorted in non-decreasing order
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.