Skip to content

Instantly share code, notes, and snippets.

@drem-darios
Created February 14, 2020 07:05
Show Gist options
  • Save drem-darios/20d304925ebb6f5b5d5b4b7b77dda926 to your computer and use it in GitHub Desktop.
Save drem-darios/20d304925ebb6f5b5d5b4b7b77dda926 to your computer and use it in GitHub Desktop.
Given a sorted array, create a new array containing squares of all the number of the input array in the sorted order.
def make_squares(arr):
squares = [0 for x in range(len(arr))]
frontPointer = 0
backPointer = len(arr) - 1
for resultIndex in range(len(arr)):
frontSquare = arr[frontPointer] * arr[frontPointer]
backSquare = arr[backPointer] * arr[backPointer]
if frontSquare >= backSquare:
squares[backPointer - frontPointer] = frontSquare
frontPointer += 1
else:
squares[backPointer - frontPointer] = backSquare
backPointer -= 1
return squares
def main():
print("Squares: " + str(make_squares([-2, -1, 0, 2, 3])))
print("Squares: " + str(make_squares([-3, -1, 0, 1, 2])))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment