Source Code:https://thecodingsimplified.com/square-a-sorted-array-in-sorted-way/
Solution-1: When we need to create new array
- We'll initialize new array of same length
- Take two variable start = 0 & end = a.length - 1
- If square of value present at start is gretaer than square of value present at end, then we fill the value at end of new array & decrease the variable by 1 & increase the start
- If square of value present at start is lesser than square of value present at end, then we fill the value at end of new array & decrease the variable by 1 & decrease the end
- At last we'll return new array
Time Complexity: O(n)
Space Complexity: O(n)
Solution-2: When we need to update in existing array
- Take two variable start = 0 & end = a.length - 1
- If square of value present at start is gretaer than square of value present at end, then we swap the value & fill square of start value at end & decrease the end
- If square of value present at start is lesser than square of value present at end, then we fill the square value at end of new array & decrease the end
Time Complexity: O(n)
Space Complexity: O(1)
Do Watch video for more info
CHECK OUT CODING SIMPLIFIED
https://www.youtube.com/codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
http://thecodingsimplified.com
I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.
★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★
https://www.youtube.com/codingsimplified?sub_confirmation=1
★☆★ Send us mail at: ★☆★
Email: [email protected]