Two Pointer Solution for Two Sum II - LeetCode Challenge

🚀 Day 49 of #100DaysOfCode Today’s challenge was a clean and elegant Two Pointer problem — 🔢 LeetCode: Two Sum II – Input Array Is Sorted 📌 Problem Summary You’re given a sorted array of integers and a target value. Your task is to find two numbers such that their sum equals the target and return their 1-based indices. ⚠️ Constraint: The array is already sorted, which opens the door to an optimized approach. 🧠 My Approach: Two Pointers Instead of using a HashMap, I leveraged the sorted property: Start one pointer at the beginning Another at the end Calculate the sum: If sum is too small → move left pointer forward If sum is too large → move right pointer backward If equal → solution found 🎯 This avoids extra space and keeps the solution super efficient. ⚙️ Complexity Analysis ⏱ Time: O(n) 💾 Space: O(1) Minimal memory, maximum efficiency 💡 🔥 Key Learning Always look for hidden constraints (like sorted input!) Two Pointer technique can replace HashMaps in many cases Clean logic often beats complex code ✅ Solution accepted with excellent runtime & memory performance Another fundamental pattern locked in 🔐 On to Day 50 — halfway milestone loading… 🚀🔥 #100DaysOfCode #LeetCode #Java #TwoPointers #Arrays #ProblemSolving #DSA #CodingJourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories