Back to course home
0% completed
Vote For New Content
Is there a O(1) space complexity solution for this problem?
Suyog Jain
May 1, 2022
Is there a O(1) space complexity solution for this problem?
0
0
Comments
Comments
Design Gurus3 years ago
This algorithm runs in O(1) space, the O(N) space is required for the output array.
S
Suyog Jain3 years ago
Ya but I was wondering could this be done in-place
A
Amitrajit Manna3 years ago
this works, without using a 3rd array: def square_arr(arr): i = 0 j = len(arr) - 1
while j > i: sq_j = arr[j] * arr[j] sq_i = arr[i] * arr[i]
if( sq_i > sq_j ) : arr[i] = arr[j] arr[j]= sq_i j -= 1 elif (sq_i < sq_j): arr[j] = sq_j j -= 1 else: j -= 1
print (arr)
J
JOD Developer2 years ago
public static int[] makeSquares(int[] arr) { int n = arr.length; // int[] squares = new int[n]; // TODO: Write your code here int firstPointer = 0, lastPointer = arr.length-1; //make a loop here while(firstPointer != lastPointer){...
On this page