Advertisement

Squares of a Sorted Array - LeetCode 977 Solution

Squares of a Sorted Array - Complete Solution Guide

Squares of a Sorted Array is LeetCode problem 977, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order . Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <= 10 4 -10 4 <= nums[i] <= 10 4 nums is sorted in non-decreasing order. Follow up: Squaring each el

Detailed Explanation

The problem asks us to take a sorted array of integers (in non-decreasing order) and return a new array containing the squares of each number, also sorted in non-decreasing order. The input is a list of integers, `nums`, and the output is a list of the squares of these integers, sorted. The key constraint is that the original input array is already sorted. This allows for an optimized O(n) solution, rather than the trivial O(n log n) solution of simply squaring each element and then sorting the resulting array.

Solution Approach

The solution employs a two-pointer approach. One pointer starts at the beginning of the input array (`left`), and the other starts at the end (`right`). We compare the absolute values of the numbers pointed to by these pointers. The number with the larger absolute value will have a larger square. This square is then placed at the end of the `result` array, and the corresponding pointer is moved inwards. This process continues until the `left` and `right` pointers meet or cross each other.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty result array of the same size as the input array, filled with default values (e.g., 0 in Python/Java/C++).
  2. Step 2: Initialize two pointers, `left` to 0 and `right` to `n-1`, where `n` is the length of the array.
  3. Step 3: Initialize an index pointer, `index`, also to `n-1`. This pointer will traverse the `result` array from right to left.
  4. Step 4: While `left <= right` (meaning the pointers haven't crossed):
  5. Step 5: Compare the absolute values of `nums[left]` and `nums[right]`.
  6. Step 6: If `abs(nums[left]) > abs(nums[right])`, then `nums[left]` squared is larger. Place `nums[left] * nums[left]` into `result[index]`, increment `left`, and decrement `index`.
  7. Step 7: Otherwise, `nums[right]` squared is larger or equal. Place `nums[right] * nums[right]` into `result[index]`, decrement `right`, and decrement `index`.
  8. Step 8: After the loop completes, the `result` array will contain the sorted squares of the numbers from the input array. Return the `result` array.

Key Insights

  • Insight 1: Since the input array is sorted, the largest squares will be at either the beginning or the end of the array. Negative numbers with large absolute values become large positive squares.
  • Insight 2: A two-pointer approach can efficiently determine the largest square at each step without needing to explicitly sort the squared array afterwards.
  • Insight 3: The squared values need to be inserted into a new array from right to left, ensuring the result remains sorted in non-decreasing order.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Two Pointers, Sorting.

Companies

Asked at: Agoda, CrowdStrike, Deutsche Bank, Instacart, Ozon, PayPal, Tinkoff, Walmart Labs, Whatnot.