This question has simple solution when we take array and use sort STL. But I wanted to do this with vector and I am facing difficulty in that. Kindly explain.
Question : Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order
@igarg145
Can you give me your code and what difficulty are you facing?
do you know how to create a vector, how to insert values in the vector, and how to sort the vector?
we sort the vector using sort(v.begin(),v.end());
class Solution { public: vector sortedSquares(vector& A) { std::vector squares(A.size()); int l = 0, r = A.size() - 1, p = A.size() - 1; while (l <= r) squares[p–] = std::pow(A[(std::abs(A[l]) > std::abs(A[r])) ? l++ : r–], 2); return squares; } };
It is showing some other solution. Kindly check the link again.
:o yea wait let me create another one.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.