Skip to main content

Sort Transformed Array - Solution & Explanation

MediumPremiumFree on FleetCodeArrayMathTwo PointersSorting9 min readAsked at: Meta, Google, LinkedIn
Practice this problem

Problem Statement

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]

Example 2:

Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output: [-23,-5,1,7]

 

Constraints:

  • 1 <= nums.length <= 200
  • -100 <= nums[i], a, b, c <= 100
  • nums is sorted in ascending order.

 

Follow up: Could you solve it in O(n) time?

Approach Overview

Problem Overview: You receive a sorted integer array nums and coefficients a, b, and c. Each element must be transformed using the quadratic function f(x) = ax^2 + bx + c. The result must remain sorted in ascending order. The challenge comes from the quadratic curve changing the order of values after transformation.

Approach 1: Transform Then Sort (Brute Force) (Time: O(n log n), Space: O(n))

The most direct solution applies the quadratic transformation to every element in the array and stores the results in a new list. Once all values are computed, you sort the resulting array using a standard sorting algorithm. The implementation is straightforward: iterate through nums, compute a*x*x + b*x + c for each element, and call a sort function. While simple, this ignores the fact that the input array is already sorted and the quadratic function has predictable behavior. Because sorting dominates the runtime, the complexity becomes O(n log n) with O(n) extra space. This approach works but rarely impresses in interviews.

Approach 2: Math + Two Pointers (Time: O(n), Space: O(n))

The key observation comes from the shape of a quadratic function. If a >= 0, the parabola opens upward and the largest transformed values appear near the ends of the input array. If a < 0, the parabola opens downward and the smallest values appear near the ends. Because the original array is sorted, you can exploit this property using the two pointers technique.

Initialize one pointer at the left end and another at the right end of the array. Compute the transformed values for both elements. If a >= 0, place the larger value at the end of the result array and move the corresponding pointer inward. If a < 0, place the smaller value at the beginning and move the corresponding pointer. Continue until all elements are processed. Each element is evaluated once, giving O(n) time with O(n) space for the result array.

This approach combines mathematical insight with a classic array scanning pattern. Instead of re-sorting the transformed numbers, you construct the sorted order directly. The method relies on understanding how quadratic functions behave and how pointer comparisons preserve order.

Recommended for interviews: Interviewers expect the Math + Two Pointers solution. Starting with the transform-and-sort approach shows you understand the problem, but recognizing the parabola behavior and eliminating the sort demonstrates stronger algorithmic thinking. The optimal solution runs in linear time and is a common application of two pointers combined with simple math reasoning.

Solution

By mathematical knowledge, the graph of a quadratic function is a parabola. When a \gt 0, the parabola opens upwards and its vertex is the minimum value; when a \lt 0, the parabola opens downwards and its vertex is the maximum value.

Since the array nums is already sorted, we can use two pointers at both ends of the array. Depending on the sign of a, we decide whether to fill the result array from the beginning or the end with the larger (or smaller) values.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Transform Then SortO(n log n)O(n)Quick implementation when performance is not critical
Math + Two PointersO(n)O(n)Best choice when the input array is already sorted and you want optimal linear performance

Video Solution

360. Sort Transformed Array (Leetcode Medium)Programming Live with Larry685 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Sort Transformed Array easy or hard?
Sort Transformed Array is generally considered a medium-level problem. The brute force solution is simple, but the optimal O(n) solution requires recognizing how a quadratic function affects ordering and applying the two pointers technique correctly.
How to solve Sort Transformed Array in O(n)?
Exploit the shape of the quadratic function f(x) = ax^2 + bx + c. If a >= 0, the largest transformed values come from the ends of the sorted array, so compare both ends and fill the result from the back. If a < 0, the smallest values come from the ends, so fill from the front while moving two pointers inward.
What is the best approach for Sort Transformed Array?
The optimal approach uses Math + Two Pointers. Because the input array is already sorted and the transformation is a quadratic function, the largest or smallest results appear near the ends of the array depending on the sign of 'a'. Using two pointers from both ends allows you to construct the sorted output in O(n) time.
What data structure is used in Sort Transformed Array?
The solution mainly uses arrays along with the two pointers technique. Two indices track the left and right ends of the sorted input array while a result array is filled from either direction depending on the quadratic coefficient.
What is the time complexity of Sort Transformed Array?
The optimal solution runs in O(n) time and O(n) space. Each element is transformed once and placed into the correct position using two pointers. A simpler brute force method transforms every element and sorts the result, which takes O(n log n) time.
Sort Transformed Array Python or Java solution approach?
Both Python and Java implementations follow the same logic: evaluate the quadratic function for elements at the left and right pointers, compare results, and insert into the correct position in a result array. The pointer movement depends on whether the parabola opens upward or downward.
Is Sort Transformed Array asked at Google, Amazon, or Meta?
Sort Transformed Array has appeared in interviews at companies like Google and other large tech firms because it combines mathematical reasoning with the two pointers pattern. Interviewers use it to test whether candidates recognize structural properties instead of relying on sorting.

Ready to solve this problem?

Practice Sort Transformed Array with our built-in code editor and test cases.

Practice on FleetCode