DSA Journey — Day 22 Today I solved the Balanced Binary Tree problem and learned an important optimization concept. -> Problem: Check whether a binary tree is height-balanced (difference between left and right subtree height ≤ 1 for every node). -> Key Learning: Instead of calculating height separately for each node (which gives O(n²) complexity), we can combine height calculation + balance checking in a single DFS traversal to achieve O(n) time. Core Insight: Return -1 immediately when a subtree is unbalanced. This avoids unnecessary computations and makes the solution efficient. Why this matters: This problem taught me how to: Optimize brute force recursion Detect early stopping conditions Write cleaner recursive logic Growth Mindset: Every tree problem is improving my recursion intuition and problem-solving speed. #DSA #Java #CodingJourney #Recursion #LearningInPublic #WomenInTech
Balanced Binary Tree Problem Solved with Optimized DFS
More Relevant Posts
-
Day 61 of My DSA Journey Today I solved the Subsets problem using the Backtracking technique. 🔹 Problem: Given an integer array nums, return all possible subsets (the power set). 🔹 Key Idea: Instead of trying to generate subsets directly, I used backtracking to explore every possible combination. 💡 Approach: Start with an empty subset. At each step, add the current subset to the result. Try including each element one by one. Recursively explore further subsets. Backtrack by removing the last element to explore other possibilities. 🔁 This approach systematically explores every possible subset using a decision tree. 📊 Time Complexity: O(n × 2ⁿ) — because each element can either be included or excluded. ✨ What I Learned: Backtracking is powerful for solving combinatorial problems. The pattern of choose → explore → unchoose is very important. 💻 Practicing problems like this helps strengthen recursion and problem-solving skills. #Day61 #DSA #Backtracking #Java #CodingJourney #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 86 of My DSA Journey | Subsets – Recursion ➡️ Bit Manipulation 💡 Yesterday, I solved the Subsets problem (LeetCode 78) using recursion (backtracking). Today, I challenged myself to solve the same problem using a different approach — Bit Manipulation 🔥 🔹 What changed? Instead of recursion, I used bit masking to generate all subsets. Every number from 0 to 2^n - 1 represents a subset in binary form. 👉 Core idea: (i & (1 << j)) != 0 This checks whether the j-th element should be included in the subset. 📌 Example: nums = [1,2,3] i = 5 → binary (101) Subset → [1,3] ⚡ Key Learning: One problem can have multiple approaches Recursion = intuitive Bit manipulation = optimized & clever Strengthened my understanding of binary concepts ⏱️ Time Complexity: O(n * 2^n) 📦 Space Complexity: O(2^n) Consistency is the key — learning, applying, and improving every single day 💯 #Day87 #DSA #Java #BitManipulation #Recursion #LeetCode #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
Imagine finding the longest number streak in an unsorted array. For example: [100, 4, 200, 1, 3, 2] Can you spot the longest consecutive sequence? 🚀 Day 67/365 — DSA Challenge Solved: Longest Consecutive Sequence The goal: Find the length of the longest sequence of consecutive numbers. Example: Input [100,4,200,1,3,2] Consecutive sequence: [1,2,3,4] Output: 4 💡 My Approach I solved it in two main steps. Step 1 — Sort the array Since the numbers are unsorted, I first sorted the array using bubble sort. Example after sorting: [1,2,3,4,100,200] Step 2 — Count consecutive numbers Then I looped through the array: • If current number = previous + 1 → increase count • If numbers are equal → skip duplicates • Otherwise → reset count While traversing, I kept track of the longest sequence length. This problem reminded me: Sometimes sorting first makes pattern detection much easier. Day 67/365 complete. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 346 – Daily DSA Challenge! 🔥 Problem: ⚡ Total Hamming Distance The Hamming distance between two integers is the number of bit positions where they differ. Given an integer array nums, return the sum of Hamming distances between all pairs. 💡 Key Insight — Count Bits Column-wise Instead of comparing every pair (O(n²)), analyze each bit position independently. For a given bit: Let ones = number of elements with that bit = 1 Let zeros = n - ones Each pair of (1,0) contributes 1 to Hamming distance. Total contribution for that bit: Sum this for all 32 bits. For each bit column we count ones × zeros and add them. ⚡ Algorithm ✅ Iterate through 32 bit positions ✅ Count how many numbers have that bit set ✅ Compute contribution ones × (n − ones) ✅ Add to result ⚙️ Complexity ✅ Time Complexity: O(32 × n) ≈ O(n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this approach avoid pairwise comparison? 2️⃣ How would this change for 64-bit numbers? 3️⃣ Can this be extended to compute XOR sum of all pairs? #DSA #Day346 #LeetCode #BitManipulation #HammingDistance #Math #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 DSA Consistency – Day 48 Today’s problem: Concatenation of Consecutive Binary Numbers 🔹 Problem Idea For a given n, concatenate the binary representations of numbers from 1 → n and return the decimal value modulo (10^9 + 7). Example: 1 → 1 2 → 10 3 → 11 Concatenation → 11011 💡 Key Intuition Instead of converting numbers to strings, we can use bit manipulation: • When a number becomes a power of 2, its binary length increases by 1 • Left shift the current result by the number of bits needed • Append the current number using addition Formula used: res = ((res << bits) + i) % mod ⚡ Why this works Every time we encounter a power of 2, the binary representation grows by one bit. We track this using: (i & (i - 1)) == 0 which efficiently checks if i is a power of two. ⏱ Complexity • Time: O(n) • Space: O(1) Consistency compounds. 48 days of showing up and solving. 💪 #DSA #LeetCode #Java #BitManipulation #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 𝟮𝟴 – 𝗝𝗮𝘃𝗮 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝗼𝗹𝘃𝗶𝗻𝗴 & 𝗗𝗦𝗔 𝗝𝗼𝘂𝗿𝗻𝗲𝘆 Consistency builds capability. Today marks Day 28 of my continuous practice in Java Problem Solving and Data Structures & Algorithms. Each day is helping me strengthen my fundamentals and improve my logical thinking. 🔹 Concepts Practiced Today - Binary Search – understanding how it significantly reduces time complexity compared to Linear Search - Floor of a Number - Ceil of a Number - Finding the Span of elements - Leader in an Array 🧪 Practice Test Focus Worked on array and sub-array based logical problems such as: - Printing all elements whose index position sum is divisible by m1 and m2 - Writing a program to identify subarrays where the difference between the maximum and minimum element equals k These exercises are helping me develop a deeper understanding of algorithm efficiency, edge cases, and array traversal logic. 💡 Every problem solved is another step toward becoming a stronger developer. #Java #DSA #ProblemSolving #LearningInPublic #ProgrammingJourney #ComputerScience #DeveloperGrowth
To view or add a comment, sign in
-
-
Day 17 #SDE Practicing Binary Search on answer space with classic optimization problems. Solved: • Ship Packages Within D Days • Magnetic Force Between Two Balls Key Learning: Both problems reinforced the pattern of minimizing/maximizing an answer under constraints. The key is to define a clear feasibility function and apply binary search on the possible range of answers instead of directly searching in the array. #LeetCode #DSA #BinarySearch #Algorithms #Java #SoftwareEngineering
To view or add a comment, sign in
-
Day 23/75 — Single Number Today's problem focused on identifying the element that appears only once while all others appear twice. Initial Approach: • Count frequency of elements using a HashMap • Return the element with frequency = 1 Time Complexity: O(n) Space Complexity: O(n) Then I explored a more optimal solution using Bit Manipulation (XOR). Key Idea: a ^ a = 0 a ^ 0 = a Since duplicates cancel each other out, XOR of all elements directly gives the unique number. Optimal Complexity: Time: O(n) Space: O(1) Learning small mathematical tricks like XOR can make solutions significantly more efficient. 23/75 — continuing the DSA journey. #Day23 #DSA #LeetCode #Java #BitManipulation #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Problem Solved: Container With Most Water Today I worked on the classic “Container With Most Water” problem, a well-known challenge that helps strengthen understanding of the Two Pointer technique and array optimization. 🔍 Problem Overview We are given an array where each element represents the height of a vertical line. The goal is to identify two lines that together form a container capable of holding the maximum amount of water. The water stored depends on the distance between the lines (width) and the minimum height of the two lines. 💡 Approach Used Instead of checking every possible pair (which would take O(n²) time), the problem can be solved efficiently using the Two Pointer approach: Start with one pointer at the beginning of the array and another at the end. Calculate the water that can be stored between the two lines. Move the pointer pointing to the smaller height, since the container height is limited by the shorter line. Continue updating the maximum area until the pointers meet. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) 🎯 Key Learning This problem highlights how smart pointer movement and observation of constraints can reduce time complexity from quadratic to linear. It is a great example of optimizing brute-force solutions using efficient patterns. #DSA #Algorithms #Java #ProblemSolving #CodingPractice #TwoPointers #LeetCode #SoftwareEngineering
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