Approach:
Step 1 Take the array
Step 2 Square each element
Step 3 Sort the array
Step 4 Return result
Why this works???
After squaring values become unordered
so sorting arranges them again
Code:
class Solution(object):
def sortedSquares(self,nums):
for i in range(len(nums)):
nums[i]=nums[i]*nums[i]
nums.sort()
return nums
Limitation:
Uses sorting so takes more time but
not optimal compared to two pointer method
Top comments (0)