-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-7.py
More file actions
34 lines (28 loc) · 789 Bytes
/
Problem-7.py
File metadata and controls
34 lines (28 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
Problem 7 : Sorted square array
'''
"Algorithm-1 using sort method O(nlogn)"
nums = [-6,-5,-4,1,3,5]
n = len(nums)
output_arr = []
def sort_square_arr(nums,n):
for i in nums:
output_arr.append(i*i)
output_arr.sort()
return output_arr
print('Input : ', nums,"\nOutput: ",sort_square_arr(nums,n))
"Algorithm-2 using two pointers O(n)"
def sort_square_arr_optimal(nums,n):
left = 0
right = n-1
output = [0]*n
for i in range(n-1,-1,-1):
if abs(nums[left]) > nums[right]:
output[i] = nums[left]*nums[left]
left = left+1
else:
output[i] = nums[right]*nums[right]
right = right-1
return output
print('-'*30)
print('Input : ', nums,"\nOutput: ",sort_square_arr_optimal(nums,n))