Understanding the Two Pointers Technique: Sorted Squares Problem
Line by Line breakdown
The two pointers technique is a fundamental approach for solving various coding challenges. Its effectiveness is often evaluated using Big O notation, which measures time and space complexity.
In this post, we'll apply the two pointers technique to the Sorted Squares problem from LeetCode. We will also examine how to analyze the time and space complexity of the solution.
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]. Sorting it gives[0, 1, 9, 16, 100].
Example 2:
Input:
nums = [-7,-3,2,3,11]Output:
[4, 9, 9, 49, 121]
Python Solution Using Two Pointers
Here's how you can solve this problem using Python:
def sortedSquares(nums: List[int]) -> List[int]:
n = len(nums)
result = [0] * n
left, right = 0, n - 1
for i in range(n - 1, -1, -1):
if abs(nums[left]) < abs(nums[right]):
square = nums[right]
right -= 1
else:
square = nums[left]
left += 1
result[i] = square * square
return resultLine-by-Line Breakdown
def sortedSquares(nums: List[int]) -> List[int]:
Defines a function that takes a list of integers and returns a list of integers.n = len(nums)
Calculates the length of the input list.result = [0] * n
Initializes a listresultof lengthnwith all elements set to 0.left, right = 0, n - 1
Sets up two pointers,leftat the start andrightat the end of the list.for i in range(n - 1, -1, -1):
Iterates backwards through theresultlist.if abs(nums[left]) < abs(nums[right]):
Compares the absolute values of the elements at theleftandrightpointers.square = nums[right]
Assigns the value at therightpointer tosquareif its absolute value is larger.right -= 1
Moves therightpointer leftward.else:
If the absolute value atleftis larger or equal.square = nums[left]
Assigns the value at theleftpointer tosquare.left += 1
Moves theleftpointer rightward.result[i] = square * square
Squares the value and stores it in theresultlist at indexi.
Time and Space Complexity Analysis
The time complexity of the sortedSquares function is O(n), where n is the length of the input list nums. This is because the function iterates over the input list once using the left and right pointers, performing constant-time operations at each step.
The space complexity is O(n) as well, since the function creates a new list
resultof the same length as the input listnumsto store the squared and sorted elements.Here's a more detailed analysis:
Time Complexity:
The line
n = len(nums)takes O(1) time since it's a constant-time operation to get the length of a list.The line
result = [0] * ntakes O(n) time to create a new list of lengthn.The loop
for i in range(n - 1, -1, -1):iteratesntimes.Inside the loop, the operations
abs(nums[left]),abs(nums[right]),square = nums[right],right -= 1,square = nums[left],left += 1, andresult[i] = square * squareall take constant time O(1).Therefore, the overall time complexity is O(n) + O(n) = O(n).
Space Complexity:
The function creates a new list
resultof lengthn, which takes O(n) space.No other additional data structures are used that would increase the space complexity beyond O(n).
In summary, the time complexity of the
sortedSquaresfunction is O(n), and the space complexity is O(n), where n is the length of the input listnums.

