Squares of a sorted array, then the k smallest squares

0
Remix

You get an integer array sorted in non-decreasing order; it may hold negative numbers.

  1. Return the squares of all its elements in sorted order, faster than sorting again (O(n) with two pointers).
  2. Return only the k smallest squares, in order.
  3. 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.

UberCodingLast asked Sep 2026
No answer yet. Write the first one; the best answer gets endorsed.