Optimizing Two Sum Problem with HashMap

Day 10: 90-Day Coding Challenge 🚀 Continuing the journey by strengthening my fundamentals in array-based problems and improving problem-solving efficiency. Today’s problem focused on the classic Two Sum problem. The goal was to find two indices in an array such that their values add up to a given target. At first, a brute-force approach using nested loops works, but it results in O(n²) time complexity, which is not efficient for larger inputs. Instead, I used an optimized approach with a HashMap (dictionary): • Iterated through the array once • Stored each number along with its index • Checked if the complement (target - current number) already exists • Returned the indices as soon as a match is found This approach reduces the time complexity to O(n) with O(n) space complexity. Today’s learning highlights: ✅ Understood the importance of optimizing brute-force solutions ✅ Learned how HashMaps improve lookup efficiency ✅ Strengthened problem-solving with single-pass solutions ✅ Improved thinking in terms of time vs space trade-offs Simple problems like these are great for building strong fundamentals and efficient thinking 💡 Excited to keep the momentum going! #90DaysOfCode #DataStructures #Algorithms #Python #CodingJourney #Arrays #HashMap

To view or add a comment, sign in

Explore content categories