Merging Sorted Arrays in-place with Two Pointers

🔥 Day 108 of My DSA Challenge – Merge Sorted Array 🔷 Problem : 88. Merge Sorted Array 🔷 Goal : Merge two sorted arrays into one sorted array in-place inside nums1. The tricky part? nums1 already has extra space at the end — and we need to fill it without using extra arrays. 🔷 Key Insight : Use the two-pointer technique starting from the end — this avoids overwriting values in nums1. Pointer p1 → end of valid elements in nums1 Pointer p2 → end of nums2 Pointer idx → last index in final merged array Place the largest element first by comparing from the end. 🔷 Approach : 1️⃣ Start from the last elements of both arrays 2️⃣ Compare and place the greater one at the end 3️⃣ Move pointers backwards 4️⃣ If nums2 still has elements, copy them This gives us O(m + n) time and O(1) extra space ✅ Sometimes the most elegant solutions are not about building new structures but smartly reusing what you already have. Two-pointer technique continues to be a powerful tool in array problems 💪 #Day108 #100DaysOfCode #LeetCode #DSA #Java #TwoPointers #Arrays #CodingChallenge #Algorithm #InPlaceAlgorithms #ProblemSolving #Programming #SoftwareEngineering #TechJourney #DeveloperJourney #CodeEveryday #LearnDSA #GrowthMindset #EngineerMindset #LogicBuilding

  • graphical user interface

To view or add a comment, sign in

Explore content categories