Day 71/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Maximum Subarray Min-Product A powerful problem combining prefix sums and monotonic stack. Problem idea: For every subarray, calculate (minimum element × sum of subarray) and find the maximum value. Key idea: Monotonic stack + prefix sum optimization. Why? • Brute force would be too slow (O(n²)) • Need to efficiently find range where each element is minimum • Prefix sum helps compute subarray sum in O(1) How it works: • Build prefix sum array • Use monotonic increasing stack • For each element, find its left & right boundary • Treat each element as the minimum of a subarray • Compute subarray sum using prefix • Multiply with element → track maximum Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Monotonic stack helps solve “nearest smaller element” problems efficiently, which unlocks many advanced patterns. 🔥 Day 71 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #MonotonicStack #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
Yogesh ..’s Post
More Relevant Posts
-
Day 76/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Lowest Common Ancestor of a Binary Tree A fundamental tree problem that builds strong recursion intuition. Problem idea: Find the lowest node in the tree that has both given nodes as descendants. Key idea: DFS + recursion (bottom-up approach). Why? • Each subtree can independently tell if it contains p or q • Combine results while backtracking • First node where both sides return non-null → answer How it works: • If current node is null / p / q → return it • Recursively search left and right subtree • If both left & right are non-null → current node is LCA • Else return the non-null side Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Tree problems often rely on post-order traversal + combining child results. Understanding this pattern unlocks many binary tree problems. 🔥 Day 76 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #Recursion #DFS #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 85/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Populating Next Right Pointers in Each Node A clean pointer-manipulation problem that strengthens tree traversal intuition without extra space. Problem idea: Given a perfect binary tree, connect each node to its next right node using a next pointer. Key idea: Level-by-level traversal using already established next pointers (no queue needed). Why? • The tree is perfect → every level is fully filled • We can use existing next links to move horizontally • This avoids extra space (unlike BFS with a queue) How it works: • Start from the leftmost node of each level • Connect left child → right child • If a next node exists, connect right child → next node’s left child • Move across the level using next pointers • Then go down to the next level Time Complexity: O(n) Space Complexity: O(1) Big takeaway: When a tree is perfect, you can often avoid extra space by cleverly using existing structure (like next pointers). 🔥 This is a powerful pattern for constant-space tree traversal problems. Day 85 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #Pointers #TreeTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 81/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Kth Smallest Element in a BST A fundamental problem that strengthens understanding of Binary Search Tree traversal. Problem idea: Find the k-th smallest element in a Binary Search Tree (BST). Key idea: Inorder Traversal (Left → Root → Right) Why? • BST property ensures sorted order during inorder traversal • Visiting nodes in this order gives elements in ascending order • The k-th visited node is your answer How it works: • Traverse the tree using inorder traversal • Keep a counter while visiting nodes • When counter == k → return that node’s value • Use stack (iterative) or recursion Time Complexity: O(h + k) Space Complexity: O(h) (stack space), where h = height of tree Big takeaway: Whenever you need sorted order from a BST, think inorder traversal. 🔥 This pattern is super important for BST-based interview questions. Day 81 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #InorderTraversal #Stacks #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 79/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Flatten Binary Tree to Linked List A powerful tree transformation problem using pointer manipulation. Problem idea: Convert a binary tree into a linked list in-place following preorder traversal. Key idea: Iterative traversal + rewiring (similar to Morris traversal idea). Why? • We need preorder sequence (Root → Left → Right) • Instead of extra space, we modify pointers in-place • Efficient and avoids recursion stack How it works: • Traverse using a pointer curr • If left child exists: → Find rightmost node of left subtree → Connect it to current’s right subtree → Move left subtree to right → Set left = null • Move to next node (curr.right) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Tree problems can often be optimized using in-place pointer rewiring, avoiding extra space. 🔥 This pattern is very useful for tree flattening and traversal optimizations. Day 79 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #MorrisTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 74/100 of #100DaysOfDSA Today’s problem was one of those that really tests your understanding of tree traversals. It wasn’t just coding — it required visualizing how different traversals relate to each other. 🔹 Construct Binary Tree from Preorder and Inorder Traversal Approach Used: Initially, it was a bit confusing how to rebuild the tree using two traversals. Key observations helped: • Preorder gives the root node • Inorder helps split the tree into left and right subtrees Used a HashMap to store inorder indices for quick lookup. Recursively built the tree by identifying the root from preorder and dividing the inorder range accordingly. The tricky part was calculating the correct index for the right subtree in preorder — once that clicked, the solution became clear. ⏱️ Time Complexity: O(n) 🧠 Space Complexity: O(n) 📌 Key Learning: Tree construction problems are all about understanding traversal relationships. Once you visualize how the tree is formed, recursion does the rest. 📌 Follow my journey as I learn, struggle, visualize, and grow — 100 days, one concept each day. Let’s learn together! #100DaysOfDSA #DSA #DataStructures #Algorithms #BinaryTree #Recursion #TreeTraversal #ProblemSolving #CodingJourney #LearnInPublic #Java #InterviewPreparation #DailyCoding #Consistency #TechCommunity
To view or add a comment, sign in
-
-
Day 115 - LeetCode Journey Solved LeetCode 236 – Lowest Common Ancestor of a Binary Tree ✅ This problem revolves around identifying the lowest common ancestor (LCA) of two given nodes in a binary tree. The LCA is defined as the lowest node in the tree that has both nodes as descendants (a node can be a descendant of itself). Approach: I used a recursive depth-first search (DFS) approach to efficiently locate the LCA. The idea is simple but powerful: • If the current node is null, return null • If the current node matches either of the target nodes (p or q), return the node • Recursively search in the left and right subtrees After recursion: • If both left and right calls return non-null values, the current node is the LCA • If only one side returns non-null, propagate that result upward This works because the first node where both targets split into different subtrees becomes the lowest common ancestor. Complexity Analysis: • Time Complexity: O(n), as we traverse each node once • Space Complexity: O(h), where h is the height of the tree (recursion stack) Key Takeaways: • Tree problems often rely on recursive decomposition 🌳 • Returning meaningful values from recursion simplifies logic • LCA is a classic concept frequently asked in interviews All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🔥 Day 140/360 – This Trick Generates All Subsets Instantly 🚀 Most people solve this using recursion… But today I learned how to generate all subsets using Bit Manipulation👇 📌 Topic: Bit Manipulation + Array 🧩 Problem: Subsets (Power Set) 📝 Problem Statement: Given an integer array, return all possible subsets (the power set). 🔍 Example: Input: [1, 2, 3] Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]] 💡 Approach: Bit Masking (Optimized) ✔ Step 1 – Calculate total subsets = 2ⁿ using (1 << n) ✔ Step 2 – Loop from 0 to 2ⁿ - 1 (each number represents a subset) ✔ Step 3 – Use bits to decide whether to include an element ⚡ Key Idea: Each number (mask) represents a subset in binary form. If a bit is ON → include that element. ⏱ Complexity: Time → O(n × 2ⁿ) Space → O(n × 2ⁿ) 📚 What I Learned: Bit manipulation can replace recursion in subset generation and makes the logic easier to visualize in binary. 🚀 Why This Matters: This concept is widely used in backtracking, DP, and interview problems. #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #BitManipulation #TechJourney #140DaysOfCode
To view or add a comment, sign in
-
-
✳️Day 35 of #100DaysOfCode✳️ 📌 Cracking the "Redundant Connection" Problem with DSU! 🛠️ My Approach: The DSU Strategy The Disjoint Set Union (or Union-Find) algorithm is perfect here because it allows us to track connected components efficiently as we iterate through the edges. ✨The Steps in my Code: 1️⃣Initialization: Created a parent array where every node is initially its own parent (representing n independent sets). 2️⃣Iterative Union: For every edge (u, v) in the input: Find the root (representative) of u. Find the root (representative) of v. Cycle Detection: * If find(u) == find(v), it means u and v are already part of the same connected component. Adding this edge would create a cycle. 🚩 Since the problem asks for the last edge that causes the cycle, I return this edge immediately. 3️⃣Union: If they are in different sets, I perform a union by setting the parent of one root to the other. 4️⃣Optimization: I used Path Compression in the find function to keep the tree flat, ensuring almost constant time complexity. 💡 When should you use DSU? 🔅DSU is a powerhouse for specific graph scenarios. Reach for it when: Cycle Detection: You need to check if adding an edge creates a cycle in an undirected graph. 🔅Connected Components: You need to count or manage groups of connected nodes dynamically. 🔅Minimum Spanning Trees: It’s the backbone of Kruskal’s Algorithm. Grid Problems: Identifying "islands" or connected regions in a 2D matrix. #DataStructures #Algorithms #LeetCode #Java #GraphTheory #CodingLife #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 566 of #750DaysOfCode 🚀 🔍 Problem Solved: Mirror Distance of an Integer Today’s problem was simple but a great reminder of how powerful basic number manipulation can be. 💡 Problem Insight: We define the mirror distance of a number as: 👉 abs(n - reverse(n)) So the task is: Reverse the digits of the number Compute the absolute difference 🧠 Approach: Extract digits using % 10 Build the reversed number Compute absolute difference 📊 Complexity: Time: O(d) → where d = number of digits Space: O(1) 🔥 Key Takeaway: Even the easiest problems reinforce fundamentals like digit extraction, reversal, and absolute difference — building blocks for more complex algorithms. #Day566 #750DaysOfCode #LeetCode #Java #CodingJourney #ProblemSolving #Algorithms #BeginnerFriendly
To view or add a comment, sign in
-
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