✳️Day 20 of #100DaysOfCode✳️ 🚀 New Problem Solved: Largest Subarray with 0 Sum! 🛠️ The Logic Behind the Code: To solve this efficiently, I used a HashMap to track prefix sums and their first occurrences. Here are the key steps I followed: Prefix Sum Tracking: As I iterate through the array, I maintain a running sum of the elements. The Base Case: If the sum itself becomes 0 at any point, the subarray from the very beginning to the current index i has a sum of 0. I update the max length to i + 1. Hashing for Efficiency: If the current sum has been seen before in the HashMap, it means the elements between the previous occurrence and the current index must add up to 0. I calculate the distance (current\_index - previous\_index) and update my maxLen. Storing New Sums: If the sum is new, I store it in the map with its index to ensure I always calculate the largest possible distance later. Complexity: Time: O(n) — We only traverse the array once. Space: O(n) — To store the prefix sums in the Map. #DataStructures #Algorithms #Java #CodingLife #GeeksforGeeks #ProblemSolving #SoftwareEngineering
Efficiently Finding Largest Subarray with 0 Sum in Java
More Relevant Posts
-
🚀 Day 55/100: LeetCode Challenge – Path Sum (Binary Trees) Halfway through the 100-day journey and the momentum is only building! Today’s focus was on Binary Trees and recursion with the "Path Sum" problem. 💡 The Logic The goal is to determine if a tree has a root-to-leaf path that sums up to a specific targetSum. My approach used a recursive Depth-First Search (DFS): Base Case: If the node is null, return false. Leaf Check: If we reach a leaf node, check if the remaining sum equals the node's value. Recursion: Subtract the current node's value from the target and move down to the left and right children. 📊 Results Runtime: 0 ms (Beats 100.00% of Java users) ⚡ Memory: 44.74 MB (Beats 89.55% of Java users) 🧠 Recursive solutions for trees always feel like magic when they click. It’s all about breaking down a complex structure into the simplest possible sub-problem. Onward to Day 56! #LeetCode #100DaysOfCode #Java #DataStructures #Algorithms #SoftwareEngineering #BinaryTree #CodingChallenge
To view or add a comment, sign in
-
-
🔥 Day 36 of #75DaysofLeetCode 🔥 LeetCode 437 – Path Sum III | Prefix Sum + DFS Magic! Struggled with counting paths in a binary tree that sum up to a target? 🤔 This problem looks like a typical tree traversal… but the optimal solution is 🔥 next-level thinking. 💡 Problem Insight: We need to count all paths (not necessarily from root) where the sum equals targetSum. Condition → Path must go downwards (parent → child). 🚫 Brute Force Idea: Start DFS from every node → check all paths ⏱️ Time Complexity: O(n²) (not efficient) ⚡ Optimized Approach: Prefix Sum + HashMap Instead of recomputing sums, we: ✔ Maintain a running sum (currentSum) ✔ Store previous sums in a HashMap ✔ Check if (currentSum - targetSum) exists 👉 This tells us how many valid paths end at the current node! 🧠 Core Logic: Add current node value to currentSum Check: currentSum - targetSum in map Update map before recursion Backtrack after recursion ⏱️ Complexity: Time: O(n) Space: O(n) 🚀 Key Takeaway: Whenever you see: 👉 "subarray sum = k" or 👉 "path sum = target" Think Prefix Sum + HashMap 💡 #LeetCode #DSA #Java #CodingInterview #BinaryTree #Programming #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 3/100 – LeetCode Challenge 🔍 Problem: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be inserted to maintain the sorted order. 💡 Key Concepts Used: • Binary Search • Time Complexity Optimization – O(log n) • Efficient searching in sorted arrays 📚 What I Learned: • How binary search reduces search time significantly compared to linear search • Handling edge cases when the target element is not present • Determining the correct insertion index while maintaining sorted order hashtag #100DaysOfCode #LeetCode #Java #DataStructures #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 18/100 – LeetCode Challenge Problem Solved: Reverse Linked List Today’s problem focused on reversing a singly linked list. While it looks simple, it’s a fundamental concept that tests pointer manipulation and understanding of linked structures. The idea is to iterate through the list and reverse the direction of each node’s pointer. At every step, we keep track of three things: the current node, the previous node, and the next node. We reverse the link by pointing the current node to the previous one, then move all pointers one step forward. By the end of the traversal, the previous pointer will be pointing to the new head of the reversed list. Time Complexity: O(n) Space Complexity: O(1) Performance: Runtime 0 ms Key takeaway: Linked list problems are all about pointer control. Mastering how pointers move and change direction is essential for solving more complex problems efficiently. Day 18 completed. Staying consistent and reinforcing core data structure fundamentals. #100DaysOfLeetCode #Java #LinkedList #Algorithms #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 37 of #75DaysofLeetCode 🚀 LeetCode 1372 – Longest ZigZag Path in a Binary Tree Just solved an interesting tree problem that really tests your understanding of DFS + state tracking 🌳 🔍 Problem Insight: A ZigZag path alternates between left ↔ right at every step. The goal is to find the longest such path in a binary tree. 💡 Key Idea: At every node, we track: Current direction (left or right) Current path length We have two choices: Continue the ZigZag → increase length Restart from opposite direction → reset length 🧠 Approach (DFS): Use recursion to explore both directions Keep updating a global maximum Try starting from both left and right 📊 Complexity: Time: O(N) Space: O(H) (recursion stack) 🔥 Takeaway: This problem is a great example of how tracking state in recursion can simplify complex tree traversals. #LeetCode #DataStructures #BinaryTree #DSA #CodingInterview #Java #100DaysOfCode
To view or add a comment, sign in
-
-
Today I solved LeetCode 100 – Same Tree. 🧩 Problem Summary: Given the roots of two binary trees p and q, check whether they are identical (same tree). Two trees are considered the same if: They are structurally identical. Corresponding nodes have the same values. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree comparison 🧠 Approach: If both nodes are null, return true. If one is null and the other is not, return false. If the values of both nodes are different, return false. Recursively compare: Left subtree of both trees. Right subtree of both trees. If all checks pass, the trees are identical. 📚 What I Learned: How to compare two binary trees recursively. Handling base cases carefully in recursion. Strengthening DFS concepts in tree problems. Building a strong foundation for advanced tree problems. Consistency in practice leads to mastery . Learning something new every day #LeetCode #DSA #BinaryTree #SameTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 15 of LeetCode Problem Solving Solved today’s problem — LeetCode #49: Group Anagrams 💻🔥 ✅ Approach: HashMap + Sorting ⚡ Time Complexity: O(n * k log k) 📊 Space Complexity: O(n * k) The task was to group strings that are anagrams of each other. 👉 I used a HashMap where: Key = sorted version of string Value = list of anagrams 💡 Key Idea: If two strings are anagrams, their sorted form will be the same. 👉 Core Logic: Convert string → char array Sort the array Use sorted string as key Store original string in map 💡 Key Learning: Transforming data (like sorting strings) can help in identifying patterns and grouping efficiently. Consistency is the key — learning something new every day 🚀 #Day15 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #100DaysOfCode
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