← Questions
Squares of a sorted array, then the k smallest squares
0
I was asked this
No reports yet. Sign in to say where you were asked it.
You get an integer array sorted in non-decreasing order; it may hold negative numbers.
- Return the squares of all its elements in sorted order, faster than sorting again (O(n) with two pointers).
- Return only the
ksmallest squares, in order. - Follow-up: find the k-th smallest square without building the squared list, in O(log n).
One report asked for the original numbers ordered by their squares instead of the squares themselves; ties could go either way.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
