🚀 Day 16 of my DSA Journey (LeetCode + Java) Today was all about Binary Search and its powerful variations. All three problems looked simple at first, but required careful observation and multiple dry runs before finalizing the logic. ✅ Problems Solved Today: LC 69 – Sqrt(x) LC 162 – Find Peak Element LC 153 – Find Minimum in Rotated Sorted Array 🧠 My Experience Solving These: 🔸 69. Sqrt(x) At first, I thought this was going to be very easy… But it turned out tricky. I used a while loop with binary search, checked mid * mid, and after multiple dry runs finally got the correct logic. Handled overflow using (long)mid * mid. 🔸 162. Find Peak Element This one was fairly easy. Used binary search + mid comparison with mid + 1. If right side is increasing → move right Else → search left. Finally returned the left pointer. 🔸 153. Find Minimum in Rotated Sorted Array Very similar to the previous one. Binary search + comparing arr[mid] with arr[right]. Left or right movement automatically narrowed down the minimum element. Clean and elegant solution. 📌 Key Takeaway Today: Binary Search is much more than searching a number. It’s about understanding patterns: ✔ Compare mid with mid+1 or right ✔ Use sorted halves intelligently ✔ Shrink the search space correctly ✔ Trust the pointers—left and right always move toward the answer Mastering these patterns makes many “hard-looking” problems simple. 💪 Step-by-step consistency. Every day I’m building stronger logic. On to Day 17! 🚀 #DSA #LeetCode #Java #BinarySearch #ProblemSolving #CodingJourney #Consistency #LearningDaily
Day 16 of DSA Journey: Binary Search Variations
More Relevant Posts
-
🚀 Day 21 of my DSA Journey (LeetCode + Java) Today’s practice focused on Array Manipulation + Counting Frequency problems. All three questions looked simple at first, but each required a deeper thought process to get the most optimized solution. ✅ Problems Solved Today LC 66 – Plus One LC 88 – Merge Sorted Array LC 1189 – Maximum Number of Balloons 🧠 My Experience Solving These: 🔸 66. Plus One This problem was trickier than I expected. Initially, I solved it in a non-optimized way. Then I realized the key insight: Traverse from the end If digit < 9, just increment and return If digit is 9 → make it 0 and continue If all digits are 9 → create new array with leading 1 Once I understood this, I solved it in one single loop—clean and efficient. 🔸 88. Merge Sorted Array My first approach used two loops and sorting, but it was: Extra time Not optimal Then I optimized it by using three pointers: i at the end of arr1 j at the end of arr2 k at the final position Compare & place larger element → move pointers. Finally append remaining elements from arr2. This method was fully optimized (O(n)) without any extra space. 🔸 1189. Maximum Number of Balloons This one was fun! I used a HashMap to count frequency of characters. The tricky part was: How to divide counts correctly for repeated characters like 'l' and 'o' After several dry runs, the logic became clear: Count freq of each char in text Count required freq for word “balloon” Divide actual/required → minimum of all divisions = answer A clean and optimal solution. 📌 Key Takeaway Today Even simple array problems can become tricky if we miss the pattern. Today I reinforced: ✔ Think from the end for digit problems ✔ Use three pointers for merging arrays ✔ Frequency-based problems → always check min(actual/required) ✔ Dry run is your best friend Step-by-step progress every day is building my confidence and logical thinking. 💪 On to Day 22! 🚀 #DSA #LeetCode #Java #Arrays #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 17 of my DSA Journey (LeetCode + Java) Today was all about Prefix Sum, Two-Pass Array Logic, and HashMap-based subarray counting. All three problems helped me think more optimally and understand how preprocessing reduces time complexity. ✅ Problems Solved Today: LC 303 – Range Sum Query: Immutable LC 238 – Product of Array Except Self LC 560 – Subarray Sum Equals K 🧠 My Experience Solving These: 🔸 303. Range Sum Query – Immutable This problem looked simple, but the real trick was understanding how to build logic using a constructor. I learned how prefix sum converts multiple queries into O(1) time. Precomputing prefix sums in the constructor gave me a real-world feel of initialization + efficient querying. 🔸 238. Product of Array Except Self Initially, I solved it in a less optimal way. Then I changed my mindset to the optimal approach: ✔ Left pass → store product before index ✔ Right pass → multiply product after index ✔ No division, no extra space (only result array) After that shift in thinking, the solution became very clean and efficient. 🔸 560. Subarray Sum Equals K This was the most insightful problem of the day. Using a HashMap + prefix sum, I learned how to find subarrays that sum to K in O(n) time: ✔ Maintain cumulative sum ✔ Check if (sum − k) already occurred ✔ If yes → increment count ✔ Store frequencies for later use A beautiful example of how maps help optimize brute-force logic. 📌 Key Takeaway Today: Prefix sums and maps are extremely powerful when combined. With the right preprocessing and logic: O(n²) problems become O(n) Hard problems become intuitive Logic becomes cleaner and more structured Every day, solving new patterns is improving my confidence and problem-solving instincts. 💪 Step-by-step, getting better. On to Day 18! 🚀 #DSA #LeetCode #Java #PrefixSum #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 24 of my DSA Journey (LeetCode + Java) Today’s practice focused on Binary Search + HashSet + Two Pointer — three powerful techniques used across many array problems. Each problem became easier once I recognized the correct pattern. ✅ Problems Solved Today: LC 34 – Find First and Last Position of Element in Sorted Array LC 349 – Intersection of Two Arrays LC 167 – Two Sum II (Input Array Is Sorted) 🧠 My Experience Solving These: 🔸 34. Find First and Last Position of Element in Sorted Array At first it looked tricky, but once I understood the pattern, the solution became clear. I used Binary Search twice: First binary search → find the first occurrence Second binary search → find the last occurrence By adjusting left/right pointers based on comparisons, the problem was solved in O(log n) efficiently. 🔸 349. Intersection of Two Arrays A simple and clean problem using HashSet. Store unique elements of first array in a set Check which elements of second array exist in the set Add them to a result set to maintain uniqueness Then convert the result set to an array. Very straightforward and optimal. 🔸 167. Two Sum II – Input Array Is Sorted This was a classic Two Pointer problem. Use left and right pointers Compare their sum with target Move pointers inward based on sum Since the array was sorted, this approach solved the problem in O(n). 📌 Key Takeaway Today: Choosing the right technique makes problem solving much faster. ✔ Binary Search for finding positions ✔ HashSet for uniqueness and fast lookup ✔ Two Pointers for sorted array problems ✔ Always think about the optimal pattern first Every day I’m improving at recognizing patterns and writing cleaner solutions. On to Day 25! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 25 of my DSA Journey (LeetCode + Java) Today’s practice focused on frequency-based problems, using HashMaps, Priority Queues, and simple array traversal. Each question had a different flavor but all were fun to solve! ✅ Problems Solved Today: LC 347 – Top K Frequent Elements LC 451 – Sort Characters by Frequency LC 414 – Third Maximum Number 🧠 My Experience Solving These: 🔸 347. Top K Frequent Elements A classic frequency + heap problem. First, I created a HashMap to count occurrences Then pushed elements into a max-heap/min-heap based on frequency Extracted the top k frequent elements This problem is a great example of how HashMap + PriorityQueue together give an optimal O(n log k) solution. 🔸 451. Sort Characters by Frequency This was a fun problem because it combines strings with frequency sorting. My steps: Count frequency using HashMap Use a max-heap to sort characters by highest frequency Build the final string by repeating each character its number of times Very satisfying to see the string rearranged cleanly based on frequencies. 🔸 414. Third Maximum Number This one was the simplest among the three. I used a single pass approach: Track the largest, second largest, and third largest values Update them as we scan through the array Return the third max or highest max if not enough distinct numbers exist No sorting needed — an O(n) solution with clean logic. 📌 Key Takeaway Today: Frequency-based problems are powerful and appear everywhere. ✔ HashMap is the backbone ✔ PriorityQueue/Heap helps extract top elements ✔ Many problems reduce to counting + ordering ✔ Always aim for O(n log k) or O(n) instead of sorting everything Feeling more confident every day! On to Day 26! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 28 of my DSA Journey (LeetCode + Java) Today’s focus was completely on Sliding Window + Hashing patterns. All three problems looked different, but the core logic was about maintaining a valid window and updating the answer efficiently. ✅ Problems Solved Today: LC 3 – Longest Substring Without Repeating Characters LC 424 – Longest Repeating Character Replacement LC 1004 – Max Consecutive Ones III 🧠 My Experience Solving These: 🔸 3. Longest Substring Without Repeating Characters This is a classic sliding window + HashMap problem. Used a HashMap to store last index of characters If a character repeats, moved left pointer smartly Calculated window length using right - left + 1 Very clean logic once the window movement is clear. 🔸 424. Longest Repeating Character Replacement This one was tricky but powerful. Maintained frequency array for characters Tracked the most frequent character (maxCount) If window size – maxCount > k → shrink window Understanding why maxCount doesn’t decrease was the key insight. 🔸 1004. Max Consecutive Ones III Sliding window on array with condition. Count number of zeros in the window If zeros > k → move left pointer Always track maximum window length Simple idea, very effective solution. 📌 Key Takeaway Today: Sliding Window problems are all about maintaining a valid window. ✔ Decide what makes window invalid ✔ Expand using right pointer ✔ Shrink using left pointer ✔ Update answer at every step ✔ Avoid nested loops → O(n) always wins The more I practice, the faster I recognize patterns. 💪 Consistency + daily effort = confidence. On to Day 29 🚀 #DSA #LeetCode #Java #SlidingWindow #HashMap #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 23 of my DSA Journey (LeetCode + Java) Today’s practice was all about Sliding Window — one of the most powerful and efficient patterns for subarray and substring problems. All three problems became easy once I identified the correct window movement. ✅ Problems Solved Today: LC 209 – Minimum Size Subarray Sum LC 1456 – Maximum Number of Vowels in a Substring of Given Length LC 643 – Maximum Average Subarray I 🧠 My Experience Solving These: 🔸 209. Minimum Size Subarray Sum This problem looked complicated at first, but then I realized it can be solved using the two-pointer sliding window. Expand window using right Shrink window using left Update minimum window length whenever sum ≥ target Once I applied this approach, the solution became fully optimized. 🔸 1456. Maximum Number of Vowels in a Substring of Length k This was a clean sliding-window substring problem. First, count vowels in the first window Then slide the window by ✔ adding new char ✔ removing old char Keep track of the maximum vowel count Very efficient and elegant. 🔸 643. Maximum Average Subarray I A straightforward calculation-based problem. First window → compute initial sum Then slide the window and update sum Track maximum sum Return maxSum / k Solved in O(n) with no extra space. 📌 Key Takeaway Today: Sliding Window helps simplify problems that involve continuous subarrays / substrings. ✔ Use two pointers ✔ Expand + shrink window ✔ Update result during each move ✔ Avoid nested loops — O(n) is always better Every day I’m getting better at recognizing patterns and choosing the right technique. On to Day 24! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
To view or add a comment, sign in
-
🚀 Day 22 of my DSA Journey (LeetCode + Java) Today’s practice focused on Strings + HashMap + Character Frequency problems. All three questions required pattern-thinking and careful dry runs to avoid mistakes. ✅ Problems Solved Today: LC 205 – Isomorphic Strings LC 2194 – Cells in a Range on an Excel Sheet LC 383 – Ransom Note 🧠 My Experience Solving These: 🔸 205. Isomorphic Strings This problem looked easy at first, but it got complicated quickly. I first tried using one HashMap, but it failed because: One direction mapping is not enough Characters need to map both ways Then I created two HashMaps: map1 → s → t map2 → t → s With this, all cases passed and the solution became fully optimized. A great learning about bidirectional mapping. 🔸 2194. Cells in a Range on an Excel Sheet Initially, I didn’t understand what the problem was asking. Then I realized: Range is like "K1:L2" Columns vary (letters) Rows vary (numbers) Used a simple nested loop: Outer loop: columns Inner loop: rows Once understood, it became an easy and clean solution. 🔸 383. Ransom Note This problem was tricky because of frequency handling. I used an int[26] array to count characters of the magazine. Count frequency Reduce frequency while checking ransom note If any count < 0 → not possible Dry runs helped me catch mistakes and finalize an O(M + N) optimized solution. 📌 Key Takeaway Today: String problems are all about patterns and frequency logic. ✔ Some problems need two-way mapping ✔ Some require simple range expansion ✔ Many become easy if you use frequency arrays / HashMap ✔ Dry runs remove 90% of mistakes Every day my problem-solving mindset is improving. Consistency is building confidence! 💪 On to Day 23! 🚀 #DSA #LeetCode #Java #HashMap #Strings #CodingJourney #ProblemSolving #Consistency #LearningDaily
To view or add a comment, sign in
-
Day 11 ( DSA using Java) To strengthen my foundation in Data Structures and Algorithms, I’m committing to solving and sharing LeetCode problem in regular periods of time, along with my thought process and approach. Writing it out helps me learn better and opens the door to meaningful discussion. 🔹 Problem: Q1662 > check if two String Arrays are equivalent 🔹 Core Concept: String , String Functions. solution : public class StringLC { public static boolean arrayStringsAreEqual(String[] word1, String[] word2) { if(String.join("",word1).equals(String.join("",word2))){ return true; } return false; } public static void main(String[] args) { String[] word1 = {"ab","c"}; String[] word2 = {"a","bc"}; System.out.println(arrayStringsAreEqual(word1, word2)); } } I’ve included my solution and time–space complexity analysis. I’d love to hear how others approached this problem—were there alternative or more efficient strategies? Let’s learn, improve, and grow together. #LeetCode #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #LearningInPublic #Java
To view or add a comment, sign in
-
If–else vs switch when should you use which ? At first glance, both solve the same problem. And honestly, I used to wonder: 👉 If `if–else` already works, why even use `switch`? Is it just for readability? Turns out, the difference becomes clearer as the logic grows. 🔹 `if–else` works best when: • conditions are complex • ranges or multiple boolean checks are involved • logic isn’t just equality comparisons 🔹 `switch` shines when: • one variable maps to many fixed values • cases are mutually exclusive • readability and maintainability matter ⚡ Performance & memory (practical view): • For small logic → difference is negligible • Long `if–else` chains → sequential checks • `switch` → JVM can optimize using jump tables or lookup strategies • Memory difference → minimal and usually not a concern What happens when conditions grow? Imagine: 20 50 or 100 possible values if–else behavior Conditions are checked top to bottom Worst case: every comparison is evaluated Time complexity: O(n) How switch works under the hood (Java) When Java compiles a switch, the JVM can choose different strategies: 1️⃣ Jump Table (tableswitch) Used when case values are dense (e.g., 1–10) Direct jump to the matching case Time complexity: O(1) 2️⃣ Lookup Table (lookupswitch) Used when values are sparse (e.g., 10, 50, 100) JVM performs a fast lookup Time complexity: O(log n) 3️⃣ String switch Hash-based lookup + switch Still faster than long if–else chains 4️⃣ Memory usage if–else: simple comparisons, minimal memory switch: may create a jump table 👉 Memory difference is usually negligible 👉 Speed + clarity is the real win What really changed my perspective was understanding how `switch` works under the hood. It’s not just syntax it can give the compiler and JVM more room to optimize. 📌 My takeaway: Choose based on clarity first. But knowing what happens under the hood helps you make better decisions when code starts to scale. Part of my “learning in public” journey — one concept at a time. Curious to hear your thoughts: Do you mainly use switch for readability or performance, or both? 👇 #LearningInPublic #Java #SoftwareEngineering #Programming #Performance #CleanCode #DeveloperJourney #TechLearning #corejava
To view or add a comment, sign in
-
🚀 Day 26 of my DSA Journey (LeetCode + Java) Today’s practice covered Stack, Two-Pointer string processing, and Prefix matching. Each problem had a different pattern, so it was a great mixed-learning day! ✅ Problems Solved Today: LC 20 – Valid Parentheses LC 443 – String Compression LC 14 – Longest Common Prefix 🧠 My Experience Solving These: 🔸 20. Valid Parentheses This is a classic stack-based validation problem. My steps: Push opening brackets into stack When closing bracket appears, check top of stack If matching pair → pop Else → return false Finally, if stack is empty → parentheses are valid. A very clean and logical stack problem. 🔸 443. String Compression This problem was interesting because it relies on two-pointer string traversal. Here’s how I solved it: Use one pointer to scan the string Count how many times a character repeats Write the character + its count into the main array Maintain a separate write index It’s an optimized in-place solution, and understanding the pointer movement is the key. 🔸 14. Longest Common Prefix A very popular string problem. My approach: Take the first string as the initial prefix Compare it with every next string Trim the prefix whenever mismatch occurs Continue until the shortest valid prefix remains Simple logic but very important technique for prefix-matching problems. 📌 Key Takeaway Today: Working on mixed patterns like Stack, String Compression, and Prefix Matching helps build versatility. ✔ Stack is perfect for matching pairs ✔ Two-pointer technique makes string processing efficient ✔ Prefix trimming is a powerful string strategy Every day I’m building stronger intuition toward string and stack problems. On to Day 27! 🚀 #DSA #LeetCode #Java #SlidingWindow #ProblemSolving #CodingJourney #Consistency #LearningDaily
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