FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Squares of a Sorted Array Using Two Pointers
NewsWeb Development

Squares of a Sorted Array Using Two Pointers

via Dev.toSandhya Steffy M4h ago

Problem Statement: Given a sorted array of integers, return a new array containing the squares of each number, also sorted in non-decreasing order. Example: Input: [-4, -1, 0, 3, 10] Output: [0, 1, 9, 16, 100] Approach: We use the two-pointer technique. Since the largest square can come from either the leftmost or rightmost element, we compare both and place the larger square at the end of the result array. Code: def sortedSquares(nums): n = len(nums) result = [0] * n left = 0 right = n - 1 pos = n - 1 while left <= right: if abs(nums[left]) > abs(nums[right]): result[pos] = nums[left] ** 2 left += 1 else: result[pos] = nums[right] ** 2 right -= 1 pos -= 1 return result Explanation: The algorithm compares the absolute values from both ends and fills the result array from the back, ensuring sorted order. Time Complexity: O(n), single pass Space Complexity: O(n), for result array

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles

The data from 400,000 developers exposes the grind myth — and shows what actually separates good…
News

The data from 400,000 developers exposes the grind myth — and shows what actually separates good…

Medium Programming • 29m ago

News

Why your next mobile app is probably headless

Lobsters • 37m ago

Major SteamOS update adds support for Steam Machine, even more third-party hardware
News

Major SteamOS update adds support for Steam Machine, even more third-party hardware

Ars Technica • 47m ago

News

Is Composer 2 in Cursor Any Good?

Medium Programming • 48m ago

Unreal Engine Hotkeys You Should Already Be Using!
News

Unreal Engine Hotkeys You Should Already Be Using!

Medium Programming • 54m ago

Discover More Articles