Solving LeetCode 977 with Two Pointers Technique

📌 Day 23/100 – Squares of a Sorted Array (LeetCode 977) 🔹 Problem: Given a sorted integer array (may contain negatives), return a new array of the squares of each number, sorted in non-decreasing order. 🔹 Approach (Two Pointers): Since negative values become positive when squared, the largest values will be at either end of the array. So we: Use left and right pointers Compare squares → place the larger one at the end of result array Move pointers inward and fill result from back to front This avoids sorting again and keeps the solution O(n). 🔹 Time & Space Complexity: ⏱️ Time: O(n) 💾 Space: O(n) (result array) 🔹 Key Learnings: ✅ Two-pointer technique helps avoid extra sorting ✅ Always think about value transformation (like negative → squared) ✅ Filling from the back is a smart trick when maintaining sorted order #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney #TwoPointers #Programming

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories