Order Transformed Values of a Sorted Array
Heaps, Sorting & SearchAuto-graded · 23 test casesLast updated
Apply a quadratic mapping to a sorted list of numbers and return the sorted results in O(n) time.
You are given a list of integers nums arranged in non-decreasing order, alongside three integer coefficients a, b, and c defining the polynomial evaluation function $f(x) = a x^2 + b x + c$.
Compute the value of $f(x)$ for every entry in nums and return a new array containing these transformed values sorted in non-decreasing order.
Your solution must run in $\mathcal{O}(n)$ time, where $n$ is the length of nums.
Constraints
- $1 \le \text{len}(nums) \le 10^5$
- $-1000 \le nums[i] \le 1000$
numsis sorted in non-decreasing order and may contain duplicate values.- $-100 \le a, b, c \le 100$
- Every intermediate and final calculated value $f(nums[i])$ fits comfortably within standard signed integer limits.
Examples
Example 1
Input: sort_transformed([-4, -2, 2, 4], 1, 3, 5)
Output: [3, 9, 15, 33]
Example 2
Input: sort_transformed([-4, -2, 2, 4], -1, 3, 5)
Output: [-23, -5, 1, 7]
Example 3
Input: sort_transformed([-1, 0, 0, 3], 0, -2, 1)
Output: [-5, 1, 1, 3]
Hints
Hint 1
A quadratic function $f(x) = ax^2 + bx + c$ represents a parabola. Consider how the function values behave at the extremes of an interval depending on the sign of $a$.
Hint 2
When $a > 0$, the parabola opens upwards, meaning the largest values occur at the boundaries (outermost unvisited elements). When $a < 0$, it opens downwards, so the smallest values occur at the boundaries.
Hint 3
Use two pointers starting at both ends of `nums`. Compare their evaluated outputs to populate the result array either from right-to-left (for $a > 0$) or left-to-right (for $a < 0$). Handle $a = 0$ as a special linear case.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sorting $n$ arbitrary elements directly takes $\mathcal{O}(n \log n)$ time. However, because nums is already sorted and $f(x) = ax^2 + bx + c$ has a monotonic or parabolic shape, we can achieve $\mathcal{O}(n)$ time by utilizing a two-pointer technique.
-
When $a > 0$ (Convex Parabola): The curve opens upward. The largest values of $f(x)$ over any remaining subsegment $[i, j]$ will always be located at one of the endpoints (
nums[i]ornums[j]). We can fill our output array from right to left (largest to smallest) by repeatedly picking the larger of $f(nums[i])$ and $f(nums[j])$, then advancing the corresponding pointer. -
When $a < 0$ (Concave Parabola): The curve opens downward. The smallest values of $f(x)$ over any remaining subsegment $[i, j]$ will always lie at one of the endpoints. We can fill our output array from left to right (smallest to largest) by picking the smaller of $f(nums[i])$ and $f(nums[j])$, then advancing the corresponding pointer.
-
When $a = 0$ (Linear Function): $f(x) = bx + c$ is strictly monotonic. Simply calculate $f(x)$ for all elements in
nums. If $b < 0$, the sequence becomes inverted, so we reverse the result. Otherwise, it is already sorted.
Complexity
- Time Complexity: $\mathcal{O}(n)$. Every step processes and positions exactly one element using constant-time calculations.
- Space Complexity: $\mathcal{O}(n)$ auxiliary space to store and return the resulting sorted array.
def sort_transformed(nums: list[int], a: int, b: int, c: int) -> list[int]:
def f(x: int) -> int:
return (a * x + b) * x + c
n = len(nums)
if a == 0:
vals = [f(x) for x in nums]
if b < 0:
vals.reverse()
return vals
res = [0] * n
left, right = 0, n - 1
if a > 0:
idx = n - 1
while left <= right:
v_left = f(nums[left])
v_right = f(nums[right])
if v_left >= v_right:
res[idx] = v_left
left += 1
else:
res[idx] = v_right
right -= 1
idx -= 1
else:
idx = 0
while left <= right:
v_left = f(nums[left])
v_right = f(nums[right])
if v_left <= v_right:
res[idx] = v_left
left += 1
else:
res[idx] = v_right
right -= 1
idx += 1
return res