Day 21 of My #DrGViswanathan challenge 🚀 Problem Solved: 3Sum (LeetCode 15) 📌 Problem Overview Today, I solved the 3Sum problem, where the goal is to find all unique triplets in an array whose sum equals zero. The challenge lies in efficiently finding valid triplets while avoiding duplicates. 💡 Key Learnings Learned how sorting can simplify complex array problems. Strengthened my understanding of the Two Pointers technique. Understood how to handle duplicate elements efficiently. Improved problem-solving skills for optimization from brute force to an efficient approach. 🔍 Approach First, sort the array. Fix one element at a time. Use two pointers (left and right) to find the remaining two numbers whose sum equals the negative of the fixed element. Skip duplicate values to ensure only unique triplets are included. Continue until all possible triplets are explored. ⏱ Complexity Analysis Time Complexity: O(n²) Space Complexity: O(1) (excluding the output list) 🌱 Reflection The 3Sum problem demonstrates how combining sorting with the two-pointer technique can significantly reduce complexity. It reinforced the importance of identifying patterns and optimizing brute-force solutions into efficient algorithms. #DrGViswanathanChallenge #Day21 #LeetCode #Python #Algorithms #DataStructures #TwoPointers #ProblemSolving #CodingJourney #InterviewPreparation #100DaysOfCode
3Sum Problem Solved with Two Pointers Technique
More Relevant Posts
-
🚀 LeetCode 402 – Remove K Digits Today's problem looked tricky at first, but once I identified the pattern, the solution became much more intuitive. 💡 My thought process The goal is to remove exactly k digits while making the resulting number as small as possible. Instead of deciding which digits to remove later, I processed the number from left to right. Whenever I encountered a smaller digit than the one before it, I realized the previous larger digit would only make the final number bigger. So, as long as I still had removals available, I discarded those larger digits. This greedy approach ensures that every digit kept contributes to the smallest possible number. If I still had removals left after processing all digits, I removed them from the end since those digits would have the least impact on making the number smaller. Finally, I removed any leading zeros, returning "0" if nothing remained. 🔍 A few hints that pointed me toward this approach ⭐ Remove k elements/digits to get the smallest result. ⭐ Process the sequence from left to right. ⭐ A later smaller element can make an earlier larger element unnecessary. ⭐ Think about building the lexicographically smallest sequence. Another great example of how recognizing patterns can make a Medium problem feel much simpler. #LeetCode #DSA #Python #Algorithms #MonotonicStack #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 91 of #100DaysOfCode — Finding the Minimum Common Value! 🔍💻 Today I solved a problem that demonstrates the power of the Two Pointers technique. Since both arrays are already sorted, we can efficiently find the smallest common element without using extra space. 🚀 👨💻 What I practiced today: ✅ Two Pointers Technique: Traversing two sorted arrays simultaneously ✅ Sorted Array Optimization: Leveraging the sorted order for an efficient solution ✅ Pointer Movement: Incrementing the pointer with the smaller value ✅ Edge Case Handling: Returning -1 when no common element exists ✅ Time Complexity Awareness: Achieving an O(n + m) solution with O(1) extra space 📌 Today's Task (LeetCode 2540): ✔ Given two sorted integer arrays nums1 and nums2, return the smallest common integer between them. If there is no common value, return -1. Example: Input: nums1 = [1,2,3] nums2 = [2,4] Output: 2 Explanation: The only common element between the two arrays is 2, so it is also the smallest common value. 🧠 Key Insight: Since both arrays are sorted, there's no need to compare every element with every other element. Start with one pointer at the beginning of each array: If both values are equal, you've found the smallest common value. If one value is smaller, move that pointer forward. Continue until one array is fully traversed. This approach visits each element at most once, making it both simple and efficient. 🔗 Hashtags: #100DaysOfCode #Day91 #Python #LeetCode2540 #DSA #TwoPointers #Arrays #CodingInterview #TechSkills #Programming
To view or add a comment, sign in
-
-
DAY 60 - Lowest Common Ancestor (PART 9 – Optimized Graphical Explanation) Today, I focused on the graphical explanation of the optimized Lowest Common Ancestor solution. After explaining the code in the previous part, this video slows things down visually and shows how the recursion actually traverses the tree. That is the main value of this part. Sometimes the code makes sense line by line, but the bigger picture still feels unclear. So in today’s walkthrough, I used a graphical approach to show: how the recursion goes down the tree how the left and right subtree counts are returned how the current node checks itself against A and B how the total count is formed why the node where total becomes 2 is the LCA This is also where the optimized solution starts to feel more intuitive. Unlike the brute force version, we are not building path arrays and comparing them later. Instead, each recursive call returns useful information upward, and that information helps us identify the Lowest Common Ancestor directly. That is the key optimization. I also hinted at the next step in the series: using the debugger tool to illustrate this same optimized example. So today’s lesson is really about making recursion visible. #EPI #Python #Algorithms #DataStructures #BinaryTree #LowestCommonAncestor #Recursion #CodingJourney
To view or add a comment, sign in
-
Day 45 of #75DaysCodingChallenge Today I solved the “Strange Counter” problem on HackerRank. This was an interesting problem because the counter doesn't decrease continuously—it resets after each cycle, and every new cycle starts with double the previous value. At first, I tried to identify a direct mathematical formula. Then I realized it would be easier to simulate the cycles until I found the one containing the given time. What I did: • Started with the initial cycle value of 3 • Kept moving to the next cycle while the given time was greater than the current cycle length • Reduced the time by the current cycle length • Doubled the cycle length for the next iteration • Once I found the correct cycle, calculated the counter value directly Time Complexity: • O(log t), since the cycle length doubles after every iteration, reducing the number of cycles that need to be checked. What I learned: • Breaking a problem into repeating cycles can make it much easier to solve • Not every pattern needs a complicated formula—sometimes a simple simulation is enough • Recognizing how values grow exponentially helps in designing efficient solutions #Day45 #75DaysCodingChallenge #CodingJourney #HackerRank #ProblemSolving #Python #LearningInPublic #CodeDaily #Consistency #Algorithms #PatternRecognition
To view or add a comment, sign in
-
Day 20 of My #DrGViswanathan challenge 🚀 Problem Solved: Find the Duplicate Number (LeetCode 287) 📌 Problem Overview Today, I solved Find the Duplicate Number, a medium-level problem that requires finding the repeated number in an array containing n + 1 integers, where each integer lies in the range [1, n]. The challenge was to solve it without modifying the array and using only constant extra space. 💡 Key Learnings Learned how to apply Floyd’s Tortoise and Hare (Cycle Detection) algorithm to arrays. Understood how an array can be visualized as a linked list, where each value points to the next index. Explored the concept of cycle detection and its real-world applications. Strengthened my understanding of the Pigeonhole Principle. 🔍 Approach Treat each element as a pointer to another index. Use two pointers: Slow pointer moves one step at a time. Fast pointer moves two steps at a time. The pointers eventually meet inside a cycle. Reset one pointer to the beginning and move both one step at a time. The point where they meet again is the duplicate number. ⏱ Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) 🌱 Reflection This problem demonstrated how algorithms from one domain can be cleverly applied to another. Using cycle detection on an array instead of a linked list was an insightful technique and reinforced the importance of recognizing hidden patterns in problems. #DrGViswanathanChallenge #Day20 #LeetCode #Python #Algorithms #DataStructures #ProblemSolving #CodingJourney #InterviewPreparation #100DaysOfCode
To view or add a comment, sign in
-
-
Day 12 of 30. 🔥 Only 1 out of 3 today — and it was the hardest one. That counts. Capacity To Ship Packages Within D Days — this one is a classic Binary Search on Answer problem, not on the array itself. Instead of searching for a value in the array, you binary search on the answer space — the minimum possible capacity (max single weight) to maximum (sum of all weights). For each midpoint, simulate how many days that capacity would take, and adjust the search based on whether it fits within D days or not. ✅ Accepted, 90/90 test cases. The key mindset shift today: Binary Search isn't just for finding elements — it's for finding the minimum/maximum value that satisfies a condition. Once that clicked, the approach became obvious. The other two problems I attempted didn't come together today — coming back to them tomorrow with fresh eyes. Not every day is perfect. But solving one genuinely hard problem is still a win. 💪 Thanks Anjali Kumari for the roadmap. #30DaysOfDSA #Day12 #LeetCode #BinarySearch #BinarySearchOnAnswer #Python #DSAChallenge #LearningInPublic #ProblemSolving #CodingInterview #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode – Subarray Sum Equals K Solved the Subarray Sum Equals K problem today using the Prefix Sum + HashMap approach. Instead of checking every possible subarray, I learned how maintaining a running prefix sum and storing its frequency in a HashMap helps find the answer efficiently. 📚 Key Learnings ✅ Prefix Sum helps calculate subarray sums efficiently. ✅ HashMap stores the frequency of previously seen prefix sums. ✅ If (current prefix sum - k) exists, a valid subarray has been found. ✅ This pattern is widely used in many array and hashing problems. ⚡ Complexity Time Complexity: O(n) – We traverse the array only once. Space Complexity: O(n) – In the worst case, the HashMap stores all unique prefix sums. Every DSA problem teaches a new pattern, and today's lesson was understanding how Prefix Sum + HashMap can transform a brute-force solution into an optimal one. Small improvements every day lead to big achievements. 🚀 guided by Sourabh Tiwari #LeetCode #DSA #Python #PrefixSum #HashMap #Coding #ProblemSolving #SoftwareEngineering #100DaysOfCode #LearningInPublic
To view or add a comment, sign in
-
-
Day 14 of LeetCode ✅ Today's takeaway: Prefix Sum isn't just about cumulative sums—it's about expressing the problem as an inequality. The key insight was deriving startValue + prefixSum >= 1 and realizing each prefix sum creates a minimum required starting value. Once I understood the math, the code (1 - min(prefix) or max(startValue, 1 - prefix)) stopped feeling like a trick and started making perfect sense. Today's biggest lesson: Don't memorize formulas—derive them from the problem statement. #LeetCode #Python #DSA #Algorithms #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 42 of Exploring Different DSA Patterns Today's challenge was LeetCode 503 – Next Greater Element II (Medium). 📌 Pattern - Monotonic Stack (Circular Next Greater Element) 💡 Approach Used a monotonic decreasing stack to efficiently find the next greater element. Since the array is circular, I preloaded the stack with elements from right to left to simulate the wrap-around behavior. While traversing the array from right to left, I removed all elements from the stack that were smaller than or equal to the current element. If the stack wasn't empty, the top element represented the next greater element; otherwise, the answer was -1. Finally, I pushed the current element onto the stack for future comparisons. Difference Between Next Greater Element & Next Greater Element II Although both problems use the same monotonic stack pattern, the key difference lies in the search space. 🟢 Next Greater Element (Non-Circular) Search is limited to the elements to the right of the current element. Once the last index is reached, the search ends. Therefore, the last element can never have a next greater element. Example: Input: [1, 2, 1] Output: [2, -1, -1] The last 1 has no elements to its right, so its answer is -1. 🔵 Next Greater Element II (Circular) The array is considered circular. After reaching the last index, the search continues from the beginning of the array. This increases the search space, allowing even the last element to find a greater element if one exists earlier in the array. Example: Input: [1, 2, 1] Output: [2, -1, 2] Here, the last 1 wraps around and finds 2 at the beginning of the array, making 2 its next greater element. Time Complexity: O(n) Space Complexity: O(n) Learnings Learned how a small change in the problem statement (making the array circular) changes the search space while still using the same monotonic stack pattern. #Day42 #DSA #LeetCode #MonotonicStack #NextGreaterElement #Python #ProblemSolving #CodingJourney #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
-
I quickly re-evaluated the problem, refactored the entire logic into a Jupyter Notebook utilizing Python/Pandas, and re-submitted. The result? An instant pass with a 95/100 grade from the AI-powered grading system! 🎉 Key Takeaways: Flexibility over Rigidness: Being comfortable switching between different tools (like SQL and Python) is essential for modern data analysis. Instant Feedback is Powerful: Seeing the immediate validation on the 3MTT portal highlighted the incredible efficiency of automated milestone assessments. Growth Mindset: Every hurdle is just an invitation to learn something new and improve. A big shoutout to the 3MTT NextGen Cohort and the Digital Horizon Foundation (DHF) for creating an environment that challenges us to adapt, iterate, and grow every single day. Onwards to the next milestone! 📈 #DataAnalysis #ContinuousLearning #3MTT #NextGenCohort #JupyterNotebook #Python #SQL #DataScience #GrowthMindset #TechTrends
To view or add a comment, sign in