DEV Community

Saranya R
Saranya R

Posted on

Squares of a Sorted Array

Approach Explanation o( ̄▽ ̄)ブ

  1. The array is sorted, but negatives become positive when squared.
  2. The largest square will come from either the leftmost negative or the rightmost positive.
  3. I used two pointers (left and right) to compare absolute values.
  4. I placed the larger square at the end of the result array and moved the pointer inward.
  5. I repeated until the result array was filled in sorted order.

Method Used: (/^▽^)/

  • Two-pointer technique
  • Fill from the back
  • In-place logic with extra result array
  • Single traversal
  • Constant pointer space

Top comments (0)