Solved LeetCode's Two Sum Challenge with HashMap

𝗗𝗮𝘆 𝟭𝟯/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 Solved Two Sum using HashMap for constant-time lookup. ➤ Approach (O(n), O(n) space): • Traverse the array once • For each element, calculate complement = target - nums[i] • Check if complement already exists in the map • If yes → return the stored index and current index • Otherwise, store the current number with its index ➤ Key Insight: Instead of checking every pair (O(n²)), store what you’ve seen and look up what you need. This reduces the problem to a single pass. #LeetCode #Java #DSA #HashMap #ProblemSolving #20DaysChallenge #Consistency

  • text

To view or add a comment, sign in

Explore content categories