Sort Transformed Array - Problem
Given a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax² + bx + c to each element nums[i] in the array, and return the array in a sorted order.
The quadratic function transforms each element, and the resulting values need to be sorted before returning.
Note: The input array is already sorted, but after applying the quadratic transformation, the order may change depending on the coefficients.
Input & Output
Example 1 — Upward Parabola
$
Input:
nums = [-4,-2,2,4], a = 1, b = 3, c = 5
›
Output:
[3,9,15,33]
💡 Note:
f(x) = x² + 3x + 5. f(-4) = 16-12+5 = 9, f(-2) = 4-6+5 = 3, f(2) = 4+6+5 = 15, f(4) = 16+12+5 = 33. Sorted: [3,9,15,33]
Example 2 — Downward Parabola
$
Input:
nums = [-2,-1,0,1,2], a = -1, b = 2, c = 1
›
Output:
[-3,-1,1,1,-3]
💡 Note:
f(x) = -x² + 2x + 1. f(-2) = -4-4+1 = -7, f(-1) = -1-2+1 = -2, f(0) = 1, f(1) = -1+2+1 = 2, f(2) = -4+4+1 = 1. Sorted: [-7,-2,1,1,2]
Example 3 — Linear Case
$
Input:
nums = [1,2,3], a = 0, b = 2, c = 1
›
Output:
[3,5,7]
💡 Note:
f(x) = 2x + 1 (linear). f(1) = 3, f(2) = 5, f(3) = 7. Already sorted since input is sorted and coefficient is positive.
Constraints
- 1 ≤ nums.length ≤ 200
- -100 ≤ nums[i] ≤ 100
- -104 ≤ a, b, c ≤ 104
- nums is sorted in non-decreasing order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted array nums and quadratic coefficients a, b, c
2
Transform
Apply f(x) = ax² + bx + c to each element
3
Sort
Return transformed values in sorted order
Key Takeaway
🎯 Key Insight: Parabolas have extreme values at the ends - use two pointers to build result in O(n) time instead of O(n log n) sorting
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code