<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing</strong> order, return <em>an array of <strong>the squares of each number</strong> sorted in non-decreasing order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-4,-1,0,3,10]
<strong>Output:</strong> [0,1,9,16,100]
<strong>Explanation:</strong> After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-7,-3,2,3,11]
<strong>Output:</strong> [4,9,9,49,121]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code><span>1 &lt;= nums.length &lt;= </span>10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Squaring each element and sorting the new array is very trivial, could you find an <code>O(n)</code> solution using a different approach?
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        //find the guy or guy just greater than the guy
        int s=0;
        int e=nums.size()-1;
        int ans = INT_MAX;

        while(s<=e){
            int mid = s + (e-s)/2;

            if(nums[mid] == target){
                return mid;
            }

            if(nums[mid] < target){
                s = mid+1;
            }
            if(nums[mid] > target){
                ans = min(ans , mid);
                e = mid-1;
            }
        }

        if(ans == INT_MAX) return nums.size();

        return ans;
    }

    void print(vector<int> &nums){
        for(int i:nums){
            cout<<i;
        }
        cout<<endl;
    }

    vector<int> sortedSquares(vector<int>& nums) {
        for(int i=0;i<nums.size();i++){
            if(nums[i]>=0) break;

            int index = searchInsert(nums, abs(nums[i]));
            nums.insert(nums.begin() + index, abs(nums[i]));
        }

        int j = 0;
        
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] >= 0) {
                nums[j] = nums[i];
                ++j;
            }
        }

        nums.resize(j);

        for(int &i:nums){
            i = i*i;
        }

        return nums;
    }
};
28900