Pick the fix
Squares must come out sorted. Which fill order works with two pointers in one pass?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Fill the output from the back with the larger end square
Right: the largest remaining square is always at one of the two ends.
Only the maximum is guaranteed to sit at an end of a sorted list with negatives, so the output is built from its largest slot down.
Why not the others?
- B Fill from the front with the smaller end square. The smallest square is near zero, usually in the middle, not at an end.
- C Fill from the front, then reverse at the end. That still needs the smallest square first, which the ends do not give you.