Problem 398314 · easy · Phase 03 Linear Management & Searching

Sorted Distances From a Point

two pointers · sorted arrays · merging

Given a list of integers nums sorted in non-decreasing order and an integer c, return a list containing abs(x - c) for every x in nums, sorted in non-decreasing order. Do not call sort or sorted.

Examples

Input:  nums = [-4, -1, 0, 3, 10], c = 1
Output: [1, 2, 2, 5, 9]
Explanation: the distances are 5, 2, 1, 2, 9.

Input:  nums = [1, 2, 3], c = 10
Output: [7, 8, 9]

Constraints

  • 0 <= len(nums) <= 10**5
  • Target: O(n) time, O(1) extra space beyond the output list.

Goals

  • Recognise that a sorted input becomes two sorted halves under a V-shaped transform
  • Merge from the outside in, or from the inside out, without sorting
Starting Python…