Understanding Squares of a Sorted Array
Return the squares of nums in nondecreasing order.
How it works
The largest remaining square must come from one of the two ends.
Compare absolute values at both ends. Write the larger square into the last unfilled output slot. Move the chosen boundary inward.
Try an example
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]
The largest remaining square must come from one of the two ends.
Constraints
- 0–64 sorted integers between -1,000,000 and 1,000,000.
- Duplicates and negative values are allowed.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.