Sort Transformed Array - Problem
Sort Transformed Array is a fascinating problem that combines quadratic functions with array manipulation. Given a sorted integer array nums and three integers a, b, and c, you need to apply the quadratic function f(x) = ax² + bx + c to each element in the array, then return the transformed values in sorted order.

The key insight is that applying a quadratic function to a sorted array doesn't necessarily maintain the sorted order - the parabola shape can cause values to increase or decrease depending on the coefficients. Your task is to efficiently determine the final sorted arrangement without expensive sorting operations.

Example: If nums = [-4, -2, 2, 4] and a=1, b=3, c=5, then:
• f(-4) = 1×16 + 3×(-4) + 5 = 9
• f(-2) = 1×4 + 3×(-2) + 5 = 3
• f(2) = 1×4 + 3×2 + 5 = 15
• f(4) = 1×16 + 3×4 + 5 = 33
Result: [3, 9, 15, 33]

Input & Output

example_1.py — Basic Quadratic Transform
$ Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]
💡 Note: Applying 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.py — Downward Parabola
$ Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output: [-5,-1,1,7]
💡 Note: With a = -1, parabola opens downward: f(-4) = -16-12+5 = -23, f(-2) = -4-6+5 = -5, f(2) = -4+6+5 = 7, f(4) = -16+12+5 = 1. Wait, let me recalculate: f(-4) = -16+(-12)+5 = -23, but that seems wrong. Let me fix: f(-4) = -16 + 3(-4) + 5 = -16-12+5 = -23, f(-2) = -4-6+5 = -5, f(2) = -4+6+5 = 7, f(4) = -16+12+5 = 1. Sorted: [-23,-5,1,7]. Actually let me recalculate properly: f(-4)=-1(16)+3(-4)+5=-16-12+5=-23, f(-2)=-1(4)+3(-2)+5=-4-6+5=-5, f(2)=-1(4)+3(2)+5=-4+6+5=7, f(4)=-1(16)+3(4)+5=-16+12+5=1. So sorted is [-23,-5,1,7]
example_3.py — Linear Function
$ Input: nums = [-2,-1,1,2], a = 0, b = 2, c = 3
Output: [-1,1,5,7]
💡 Note: When a = 0, function becomes linear f(x) = 2x + 3: f(-2) = -4+3 = -1, f(-1) = -2+3 = 1, f(1) = 2+3 = 5, f(2) = 4+3 = 7. Since input is sorted and function is linear with positive slope, output remains sorted.

Visualization

Tap to expand
Quadratic Transformation Visualizationf(x) = ax² + bx + c (a > 0)-4-2024Input (Sorted)591321Result (Sorted)leftright💡 Key Insight: Extremes of sorted array produce extremes of transformed array
Understanding the Visualization
1
Analyze Parabola Direction
Determine if parabola opens upward (a > 0), downward (a < 0), or is linear (a = 0)
2
Position Two Pointers
Place pointers at array extremes to access values that will be largest/smallest after transformation
3
Strategic Result Filling
Fill result array from appropriate end based on parabola characteristics
4
Optimal Traversal
Move pointers inward, always placing the next largest/smallest value correctly
Key Takeaway
🎯 Key Insight: By understanding parabola properties, we can predict that the extreme values of a sorted array will produce the extreme values after quadratic transformation, allowing us to build the sorted result efficiently with two pointers.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through array with two pointers

n
2n
Linear Growth
Space Complexity
O(n)

Only space for result array, O(1) extra space

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 200
  • -100 ≤ nums[i], a, b, c ≤ 100
  • nums is sorted in non-decreasing order
  • -104 ≤ transformed values ≤ 104
Asked in
Google 15 Meta 12 Microsoft 8 Amazon 6
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen