> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sort Transformed Array Python Solution

> Tested Python solution for LeetCode 360 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 360, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Two Pointers](/catalog/topics/two-pointers), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/sort-transformed-array/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 360   # by problem number
lcpy gen -s sort_transformed_array   # by problem name
```

## Problem

Given a **sorted** integer array `nums` and three integers `a`, `b` and `c`, apply a quadratic function of the form `f(x) = ax^2 + bx + c` to each element `nums[i]` in the array, and return *the array in a sorted order*.

### Examples

```
Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]
```

```
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?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sort_transformed_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sort_transformed_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def sort_transformed_array(self, nums: list[int], a: int, b: int, c: int) -> list[int]:
        n = len(nums)
        result: list[int] = [0] * n

        def f(x: int) -> int:
            return a * x * x + b * x + c

        i, j = 0, n - 1
        idx = n - 1 if a >= 0 else 0
        while i <= j:
            left, right = f(nums[i]), f(nums[j])
            if a >= 0:
                if left >= right:
                    result[idx] = left
                    i += 1
                else:
                    result[idx] = right
                    j -= 1
                idx -= 1
            else:
                if left <= right:
                    result[idx] = left
                    i += 1
                else:
                    result[idx] = right
                    j -= 1
                idx += 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.