# Sorted Squared Array

<figure><img src="https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-c952eec3539fbe091d8803841645409e5ae4858d%2FScreenshot%202023-01-20%20at%2015.03.45.png?alt=media" alt=""><figcaption></figcaption></figure>

```javascript
function sortedSquaredArray(arr) {

  let positiveStart = 0;
  for (let i = 0; i < arr.length; i++) {
    const curr = arr[i];
    if (curr < 0) continue;
    
    positiveStart = i;
    break;
  }

  arr = arr.map(v => v * v);

  const answer = [];
  
  let s = 0;
  let e = arr.length - 1;
  while (s <= e) {
    const start = arr[s];
    const end = arr[e];

    if (start > end) {
      answer.unshift(start);
      s++;
    } else {
      answer.unshift(end);
      e--;
    }

  }

  
  return answer;
}

// Do not edit the line below.
exports.sortedSquaredArray = sortedSquaredArray;
```
