Squares of a sorted array, then the k smallest squares
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 answer yet. Write the first one; the best answer gets endorsed.
