360. Sort Transformed Array - Detailed Explanation
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Problem Statement
Given a sorted integer array nums
of length n
and integer coefficients a
, b
, c
, define a quadratic function
f(x) = a*x^2 + b*x + c
Apply f
to each element of nums
and return the resulting array in sorted order.
Examples
- Input
nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5
Output
[3, 9, 15, 33]
Explanation
f(x) = x² + 3x + 5 → transforms to [9, 3, 15, 33] → sorted → [3, 9, 15, 33] - Input
nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5
Output
[-23, -5, 1, 7]
Explanation
f(x) = -x² + 3x + 5 → transforms to [-23, -5, 1, -7] → sorted → [-23, -5, 1, 7] - Input
nums = [0, 1, 2], a = 0, b = 2, c = -1
Output
[-1, 1, 3]
Explanation
f(x) = 2x − 1 → [−1, 1, 3] is already sorted
Constraints
n == nums.length
1 ≤ n ≤ 10⁵
-10⁴ ≤ nums[i], a, b, c ≤ 10⁴
nums
is sorted in non‑decreasing order
Brute Force Approach
- Transform & Sort
- Allocate an array
res
of lengthn
. - For each
i
in[0, n)
computeres[i] = f(nums[i])
. - Sort
res
with any O(n log n) sort.
- Allocate an array
- Complexity
- Time O(n log n) for the sort
- Space O(n) for the output array
Optimal Two‑Pointer Approach
Because nums
is sorted, and f(x)
is a parabola opening up if a>0
or down if a<0
, the largest (or smallest) transformed values lie at the ends. We can merge from the outside in:
- Setup
- Two pointers
left = 0
,right = n-1
. - Output array
res
of lengthn
. - An index
idx
pointing at either the start (ifa<0
) or end (ifa>=0
) ofres
.
- Two pointers
- Merge Loop
- While
left ≤ right
:- Compute
L = f(nums[left])
,R = f(nums[right])
. - If
a >= 0
: place the larger ofL,R
atres[idx--]
; else place the smaller atres[idx++]
. - Move the pointer (
left++
orright--
) whose value was used.
- Compute
- While
- Why It Works
- Parabola shape ensures extremes produce the extreme transformed values.
- By filling from one end inward, we maintain sorted order without extra sorting.
- Time O(n), Space O(n)
Step‑by‑Step Walkthrough
Take nums = [-4, -2, 2, 4]
, a = 1, b = 3, c = 5
- f(x) = x² + 3x + 5
- Initialize
left=0→-4
,right=3→4
,idx=3
(fill from back)
- L = f(-4)=9, R = f(4)=33 → place 33 at res[3],
right→2
- L = 9, R = f(2)=15 → place 15 at res[2],
right→1
- L = 9, R = f(-2)=3 → place 9 at res[1],
left→1
- Only
left=1
remains: L=3 → place at res[0]
Result → [3,9,15,33]
Complexity Analysis
- Time O(n) — single pass with two pointers
- Space O(n) — for output
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Edge Cases
- a = 0 → f(x) is linear → transformed array remains monotonic, can fill directly.
- n = 1 → just return
[f(nums[0])]
. - All nums equal → every f(nums[i]) same; both pointers equivalent.
Common Mistakes
- Forgetting to handle
a < 0
separately (fills from front instead of back). - Off‑by‑one errors when updating
idx
. - Re‑sorting the result after two‑pointer merge (defeats the purpose).
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.