🚀 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
Two Pointer Solution for Two Sum II - LeetCode Challenge
More Relevant Posts
-
🚀 Day 21 of #100DaysOfCode 🧩 Problem: Find Median of Two Sorted Arrays (LeetCode #4, Hard) 💡 Concept: Given two sorted arrays, the goal is to find the median of the combined dataset after merging both arrays. 🛠 Approach (Merge + Sort): 1️⃣ Create a new array to store elements from both input arrays. 2️⃣ Copy all elements from nums1 and nums2 into the new array. 3️⃣ Sort the merged array using Bubble Sort to maintain order. 4️⃣ Determine the median: If total length is even, return the average of the two middle elements. If odd, return the middle element directly. 🔥 Performance: ✅ Accepted on LeetCode ⚡ Runtime: 52 ms 💾 Memory: 48.81 MB 📊 Complexity: ⏱ Time: O((m + n)²) — due to bubble sort 💾 Space: O(m + n) — extra array used for merging #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Consistency #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge 🚀 Problem: Remove Element Approach: Used two pointers Shifted non-matching elements to the front Returned the count of valid elements Time Complexity: O(n) Space Complexity: O(1) (in-place) Key takeaway: In-place array problems often reduce to index management, not extra data structures. #LeetCode #100DaysOfCode #DSA #Java #ProblemSolving #InterviewPrep #100DaysOfLeetCode
To view or add a comment, sign in
-
-
🚀 Day 76 of #100DaysOfCode Today I worked on a clean and elegant array manipulation problem — LeetCode: Rotate Array ✅ 📌 Problem Summary Given an array, rotate it to the right by k steps in-place, without using extra space. 🧠 Approach Used: Reverse Technique Instead of shifting elements one by one, I used a smart 3-step reverse strategy: Reverse the entire array Reverse the first k elements Reverse the remaining n − k elements This achieves the rotation efficiently and keeps the solution simple. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) (in-place) 🔥 Key Learnings Sometimes the best solution is about reordering operations, not adding complexity In-place algorithms are powerful when space constraints matter Reverse logic is a recurring and underrated pattern in array problems Onward to Day 77 🚀 Consistency > Motivation 💪 #100DaysOfCode #LeetCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 7 of #100DaysOfCode Solved Search a 2D Matrix on LeetCode using an optimized matrix traversal approach 🔎 🧠 Key insight: Since rows and columns are sorted, starting from the top-right corner helps eliminate either a row or a column in each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value < target → move down 🔹If value > target → move left 🔹Repeat until target is found or bounds are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🧠 Day 76 of #100DaysOfLeetCode 📌 Problem: Combination Sum II 📊 Difficulty: Medium 💡 Key Insight: Sorting the array helps handle duplicates. While backtracking, duplicates are skipped only in the “not pick” path to ensure unique combinations. 🛠️ Approach: Sort the candidates array Use backtracking to explore combinations Each number can be used once → move to index + 1 Skip duplicate elements to avoid repeated combinations ⏱️ Complexity: Time: O(2^n) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🔥 Day 305 – Daily DSA Challenge! 🔥 Problem: 📦 Subsets (Power Set) Given an integer array nums of unique elements, return all possible subsets (the power set). 💡 Key Insights: 🔹 Every element has two choices → include it or exclude it. 🔹 The total number of subsets for n elements is 2ⁿ. 🔹 Backtracking helps explore all possibilities in a clean, structured way. 🔹 Add the current subset to the result at every recursion level. 🔹 Move forward using i + 1 to avoid reusing elements. ⚡ Optimized Plan: ✅ Start with an empty subset ✅ Use backtracking to build subsets incrementally ✅ Add the current subset to results before branching further ✅ Try including each remaining element one by one ✅ Backtrack after exploring each choice ✅ Time Complexity: O(2ⁿ) ✅ Space Complexity: O(n) (recursion depth) 💬 Challenge for you: 1️⃣ How would this change if nums contained duplicate elements? 2️⃣ Can you solve this using bit masking instead of recursion? 3️⃣ How would you generate subsets of exactly k elements? #DSA #Day305 #LeetCode #Backtracking #Recursion #PowerSet #Java #ProblemSolving #KeepCoding #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 44 | LeetCode Medium 🪟 Minimum Subarray Length with Sum of Distinct Elements ≥ K Solved a sliding window + hashmap problem today that really tests window control and frequency handling. 🧠 Problem Approach (Intuition) We need the smallest subarray such that: Only distinct elements contribute to sum Total sum ≥ k So instead of recalculating every subarray: 👉 We use a sliding window with a frequency map 🔍 Key Strategy Expand window using pointer a Track frequencies using HashMap Add to sum only when an element appears for the first time Shrink window from left (b) when sum ≥ k Update minimum length dynamically ✨ Why this works well Each element enters & exits window once → O(n) HashMap ensures distinct sum tracking No nested loops brute force ✅ Key Learning Sliding Window becomes powerful when combined with frequency maps. This pattern appears a lot in: Subarray problems Distinct elements Minimum/maximum window questions 📌 Final Thought DSA is less about memorizing solutions and more about: recognizing patterns and controlling state efficiently. On to the next one 💪 #LeetCode #LeetCodeDaily #Day44 #SlidingWindow #HashMap #Java #DSA #ProblemSolving #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 10/30 – Strengthening String Processing Fundamentals Today’s problem focused on determining whether two strings are anagrams of each other. Instead of sorting both strings (which would take O(n log n) time), I implemented a frequency-count approach using a fixed-size array to track character occurrences. 💡 Approach First, check if the lengths differ — if so, they cannot be anagrams. Use an integer array of size 26 (for lowercase letters). Increment counts for characters in the first string. Decrement counts for characters in the second string. If all values return to zero, the strings are valid anagrams. This approach ensures: ⏱ O(n) time complexity 📦 O(1) space complexity (constant size array) 📊 Performance ✅ All test cases passed ⚡ Efficient runtime 💾 Optimized space usage 📚 Key Takeaway Optimizing from a sorting-based solution to a frequency-count approach highlights the importance of understanding constraints. Sometimes improving complexity from O(n log n) to O(n) is not about writing more code — it's about choosing the right idea. Ten days completed. Small improvements every day compound over time. #Day10 #30DaysOfCode #LeetCode #Java #Algorithms #StringManipulation #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 11/30 – Working with Numbers Without Converting to Strings Today’s problem involved checking whether a given integer is a palindrome. While one straightforward approach would be converting the number into a string and reversing it, I chose to solve it mathematically — by reversing the digits of the number itself. 💡 Approach Immediately return false for negative numbers Store the original number Reverse the digits using modulo (% 10) and division (/ 10) Compare the reversed number with the original This avoids unnecessary string conversion and keeps the logic purely numerical. 📊 Performance ✅ All test cases passed ⚡ Efficient runtime ⏱ O(log₁₀ n) time complexity 📦 O(1) space complexity 📚 Key Takeaway Sometimes the better solution isn’t the easiest one — it’s the one that respects constraints and demonstrates deeper understanding of number manipulation. Day 11 complete. Building stronger fundamentals, one problem at a time. #Day11 #30DaysOfCode #LeetCode #Java #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode – Day 12 Solved Search in Rotated Sorted Array II on LeetCode 🔄🔍 🧠 Key insight: When duplicates are present, it’s not always possible to directly identify the sorted half. If nums[left] == nums[mid], we can safely move left++ to reduce the search space and continue. ⚙️ Approach: 🔹Use modified binary search 🔹Handle duplicates by skipping equal boundary elements 🔹Identify the sorted half 🔹Check if the target lies in that range and adjust pointers accordingly ⏱️ Time Complexity: Average: O(log n) Worst case (many duplicates): O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development