Sort Transformed Array
We need to understand the shape of the quadratic function (f(x) = ax^2 + bx + c). Depending on the value of (a), the parabola could either open upwards (when (a > 0)) or downwards (when (a < 0)).
- If (a > 0), the smallest values of (f(x)) will be at the two ends of the sorted array, and the largest value will be in the middle.
- If (a < 0), the largest values of (f(x)) will be at the two ends of the sorted array, and the smallest value will be in the middle.
- If (a = 0), the function is linear and the transformation depends on the sign of (b).
Given the sorted order of nums
, we can make use of two pointers (one at the start and the other at the end) to pick the next largest or smallest value.
Here’s the solution:
|
|
The function f
is used to calculate the value of the quadratic function for any (x). Using two pointers, left
and right
, we then fill in the result
array in the desired order depending on the value of (a).