360. Sort Transformed Array
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to apply a quadratic function f(x) = ax^2 + bx + c to each element of a given sorted array nums and then return the resulting array in sorted order. A straightforward approach would be to compute f(x) for each element and then sort the resulting array. This would have a time complexity of O(n log n) due to the sorting step. The problem's follow-up challenges us to find an O(n) solution, which suggests we should leverage the fact that the input array nums is already sorted.
Solution
Sort Transformed Array in Python
class Solution:
def sortTransformedArray(self, nums: List[int], a: int, b: int, c: int) -> List[int]:
n = len(nums)
result = [0] * n
left, right = 0, n - 1
# Helper function for the quadratic transformation
transform = lambda x: a * x * x + b * x + c
if a >= 0:
# Parabola opens upwards or it's a line (a=0).
# The largest values are at the ends.
# We fill the result array from the end towards the beginning.
k = n - 1
while left <= right:
left_val = transform(nums[left])
right_val = transform(nums[right])
if left_val > right_val:
result[k] = left_val
left += 1
else:
result[k] = right_val
right -= 1
k -= 1
else: # a < 0
# Parabola opens downwards.
# The smallest values are at the ends.
# We fill the result array from the beginning towards the end.
k = 0
while left <= right:
left_val = transform(nums[left])
right_val = transform(nums[right])
if left_val < right_val:
result[k] = left_val
left += 1
else:
result[k] = right_val
right -= 1
k += 1
return result
Sort Transformed Array in Java
class Solution {
public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
int n = nums.length;
int[] result = new int[n];
int left = 0, right = n - 1;
if (a >= 0) {
// Parabola opens upwards or it's a line (a=0).
// The largest values are at the ends.
// We fill the result array from the end towards the beginning.
int k = n - 1;
while (left <= right) {
int leftVal = transform(nums[left], a, b, c);
int rightVal = transform(nums[right], a, b, c);
if (leftVal > rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k--;
}
} else { // a < 0
// Parabola opens downwards.
// The smallest values are at the ends.
// We fill the result array from the beginning towards the end.
int k = 0;
while (left <= right) {
int leftVal = transform(nums[left], a, b, c);
int rightVal = transform(nums[right], a, b, c);
if (leftVal < rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k++;
}
}
return result;
}
// Helper function to apply the quadratic transformation.
private int transform(int x, int a, int b, int c) {
return a * x * x + b * x + c;
}
}
Sort Transformed Array in C++
class Solution {
public:
vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
int n = nums.size();
vector<int> result(n);
int left = 0, right = n - 1;
// Helper lambda to calculate the quadratic function
auto transform = [&](int x) {
return a * x * x + b * x + c;
};
if (a >= 0) {
// Parabola opens upwards or it's a line (a=0)
// The largest values are at the ends
// Fill the result array from the end
int k = n - 1;
while (left <= right) {
int leftVal = transform(nums[left]);
int rightVal = transform(nums[right]);
if (leftVal > rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k--;
}
} else { // a < 0
// Parabola opens downwards
// The smallest values are at the ends
// Fill the result array from the beginning
int k = 0;
while (left <= right) {
int leftVal = transform(nums[left]);
int rightVal = transform(nums[right]);
if (leftVal < rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k++;
}
}
return result;
}
};
Sort Transformed Array in TypeScript
function sortTransformedArray(nums: number[], a: number, b: number, c: number): number[] {
const n = nums.length;
const result: number[] = new Array(n);
let left = 0, right = n - 1;
// Helper function for the quadratic transformation
const transform = (x: number): number => a * x * x + b * x + c;
if (a >= 0) {
// Parabola opens upwards or it's a line (a=0).
// The largest values are at the ends.
// We fill the result array from the end towards the beginning.
let k = n - 1;
while (left <= right) {
const leftVal = transform(nums[left]);
const rightVal = transform(nums[right]);
if (leftVal > rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k--;
}
} else { // a < 0
// Parabola opens downwards.
// The smallest values are at the ends.
// We fill the result array from the beginning towards the end.
let k = 0;
while (left <= right) {
const leftVal = transform(nums[left]);
const rightVal = transform(nums[right]);
if (leftVal < rightVal) {
result[k] = leftVal;
left++;
} else {
result[k] = rightVal;
right--;
}
k++;
}
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.