407. Sort Transformed Array
Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax2 + bx + c to each element nums[i] in the array, and return the array in a sorted order.
Example 1:
Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]
Explaination :
Step 1: Apply the quadratic function f(x) = ax² + bx + c to each element:
f(-4) = (1)(-4)² + (3)(-4) + 5 = 16 - 12 + 5 = 9
f(-2) = (1)(-2)² + (3)(-2) + 5 = 4 - 6 + 5 = 3
f(2) = (1)(2)² + (3)(2) + 5 = 4 + 6 + 5 = 15
f(4) = (1)(4)² + (3)(4) + 5 = 16 + 12 + 5 = 33
Step 2: The transformed array before sorting: [9, 3, 15, 33]
Step 3: Sorting the result: [3, 9, 15, 33]
Example 2:
Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output : [-23, -5, 1, 7]
Explanation
Step 1: Apply the quadratic function f(x) = ax² + bx + c to each element:
f(-4) = (-1)(-4)² + (3)(-4) + 5 = -16 - 12 + 5 = -23
f(-2) = (-1)(-2)² + (3)(-2) + 5 = -4 - 6 + 5 = -5
f(2) = (-1)(2)² + (3)(2) + 5 = -4 + 6 + 5 = 7
f(4) = (-1)(4)² + (3)(4) + 5 = -16 + 12 + 5 = 1
Step 2: The transformed array before sorting: [-23, -5, 7, 1]
Step 3: Sorting the result: [-23, -5, 1, 7]
Now Your Turn!
Pick the correct output for the given inputInput nums =[-32,-20,-14], a= 0 , b = -7 , c = -33
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
1 <= nums.length <= 200
-100 <= nums[i], a, b, c <= 100
nums is sorted in ascending order.