<jupyter_start><jupyter_text>Sorted Squared Array[link](https://www.algoexpert.io/questions/Sorted%20Squared%20Array)  My Solution<jupyter_code># O(n) time | O(n) space
def sortedSquaredArray(array):
    # Write your code here.
    divIdx = 0
    for i in range(len(array)):
        if array[i] >= 0:
            break
        divIdx += 1
    
    squares = [x * x for x in array]
    left = divIdx - 1
    right = divIdx
    res = []
    while left >= 0 and right < len(array):
        if squares[left] > squares[right]:
            res.append(squares[right])
            right += 1
        else:
            res.append(squares[left])
            left -= 1
    if left >= 0:
        res += squares[left::-1]
    else:
        res += squares[right:]
    return res
# O(n) time | O(n) space
def sortedSquaredArray(array):
    # Write your code here.
    res = [0 for _ in array]
    left = 0
    right = len(array) - 1
    for idx in reversed(range(len(array))):
        if abs(array[left]) > abs(array[right]):
            res[idx] = array[left] * array[left]
            left += 1
        else:
            res[idx] = array[right] * array[right]
            right -= 1
    return res<jupyter_output><empty_output><jupyter_text>Expert Solution<jupyter_code># O(nlogn) time | O(n) space - where n is the length of the input array
def sortedSquaredArray(array):
    sortedSquares  = [0 for _ in array]

    for idx in range(len(array)):
        value = array[idx]
        sortedSquares[idx] = value * value

    sortedSquares.sort()
    return sortedSquares
# O(n) time | O(n) space - where n is the length of the input array
def sortedSquaredArray(array):
    sortedSquares = [0 for _ in array]
    smallerValueIdx = 0
    largerValueIdx = len(array) - 1

    for idx in reversed(range(len(array))):
        smallerValue = array[smallerValueIdx]
        largerValue = array[largerValueIdx]

        if abs(smallerValue) > abs(largerValue):
            sortedSquares[idx] = smallerValue * smallerValue
            smallerValueIdx += 1
        else:
            sortedSquares[idx] = largerValue * largerValue
            largerValueIdx -=  1

    return sortedSquares<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11075660893345488, 'fraction_numerical': 0.010938924339106655, 'mean_word_length': 2.3665644171779143, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '518880', 'license': 'MIT', 'n_tokens_mistral': 741, 'n_tokens_neox': 704, 'n_words': 188, 'path': 'algoExpert/sorted_squared_array/solution.ipynb', 'repo_name': 'maple1eaf/learning_algorithm', 'size': '2194'}