🔥 LeetCode Day--- 4 | “Median of Two Sorted Arrays” (Hard, Java) Today’s challenge was one of those that really test your logic, patience, and understanding of binary search. This problem wasn’t about just merging two sorted arrays — it was about thinking smarter 🧠. Instead of brute-forcing through both arrays (O(m+n)), I implemented a binary partition approach to achieve O(log(min(m, n))) efficiency 💡 What I learned today: Always choose the smaller array for binary search — it makes the partition logic simpler. Handle boundaries carefully with Integer.MIN_VALUE and Integer.MAX_VALUE. The goal is to find the perfect partition where: Left half ≤ Right half Elements are balanced across both arrays Once that’s done → median can be easily calculated! ✅ Result: Accepted | Runtime: 0 ms 🚀 Hard problem turned into a logic puzzle that was actually fun to solve! 🧩 Concepts Strengthened: Binary Search Partitioning Logic Edge Case Handling Mathematical Thinking #LeetCode #Day4 #Java #BinarySearch #ProblemSolving #CodingChallenge #DataStructures #Algorithms #CodeEveryday #DeveloperJourney #TechLearning #LeetCodeHard #CodingCommunity
Solved "Median of Two Sorted Arrays" with Binary Search in Java
More Relevant Posts
-
📌 Day 16/100 - Reverse String (LeetCode 344) 🔹 Problem: Reverse a given string in-place — meaning you must modify the original array of characters without using extra space. 🔹 Approach: Used the two-pointer technique — one starting at the beginning and one at the end of the array. Swap characters at both pointers, then move them closer until they meet. Efficient, clean, and runs in linear time without additional memory allocation. 🔹 Key Learnings: In-place algorithms optimize space complexity significantly. The two-pointer pattern is a versatile tool for many array and string problems. Understanding mutable vs immutable structures in Java is crucial for memory efficiency. Sometimes, the simplest logic beats the most complex one. 🧠 “True efficiency lies in simplicity, not complexity.” #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #TwoPointers
To view or add a comment, sign in
-
-
🌳 Day 65 of #LeetCode Journey 🔹 Problem: 106. Construct Binary Tree from Inorder and Postorder Traversal 🔹 Difficulty: Medium 🔹 Language: Java Today’s problem is about rebuilding a binary tree when you’re given its inorder and postorder traversal arrays. 🧩 Key Idea: The last element in postorder is always the root. In inorder traversal, elements to the left of the root are in the left subtree, and elements to the right are in the right subtree. We recursively build the tree using this property. 💡 Approach: Start from the end of the postorder array to get the root. Use a hashmap to store inorder indices for O(1) lookup. Recursively construct the right subtree first, then the left subtree (since we’re moving backward in postorder). 🧠 Concepts reinforced: Recursion HashMap for index lookup Understanding inorder & postorder relationships 🔥 Another step forward in mastering tree construction problems! #LeetCode #100DaysOfCode #Java #CodingChallenge #DataStructures #BinaryTree #Recursion #ProblemSolving #ProgrammingJourney
To view or add a comment, sign in
-
-
Day 89 of #100DaysOfCode Solved Find Numbers with Even Number of Digits in Java 🔢 Approach The goal of this problem was to count how many integers in a given array have an even number of digits. I implemented two methods: findNumbers(int[] nums): This is the main function that iterates through every number in the input array nums. isEvenOrOdd(int n): This helper function takes an integer and determines if its digit count is even or odd. Inside the helper function, I used a while loop to count the digits: I initialized a count to 0. The loop continues as long as $n > 0$. In each iteration, I increment count and then perform integer division by 10 (n = n / 10) to remove the least significant digit. Finally, I return true if the total count of digits is even (count \% 2 == 0). This efficient, digit-by-digit checking approach resulted in a strong performance, beating 98.90% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #ProblemSolving #Optimization
To view or add a comment, sign in
-
-
🔥 Day 20 | LeetCode Challenge – Decode Ways (Problem #91) Today’s challenge involved decoding an encoded numeric string where each number represents a letter (A–Z). The task was to determine how many possible ways the message can be decoded. 💡 Core Concept: Each number from 1–26 maps to letters A–Z, and the challenge lies in handling overlapping combinations and invalid encodings (like “06”). 📘 Approach: Implemented a Dynamic Programming (DP) solution in Java. Used an integer array dp[] to store the number of decoding ways up to each position. Considered both single-digit and two-digit decodings: Single-digit → valid if between 1 and 9. Two-digit → valid if between 10 and 26. The result was stored in dp[n], representing all valid decoding combinations. ⚙️ Algorithm Used: Dynamic Programming 🧾 Language: Java ⚡ Runtime: 1 ms (Beats 83.21%) 💾 Memory: 43.33 MB 🧠 Insight: Breaking the decoding process into smaller subproblems allowed efficient computation of possible message interpretations. This problem elegantly demonstrates how DP handles overlapping substructures in combinatorial challenges. #Day20 #LeetCode #DecodeWays #DynamicProgramming #JavaDeveloper #100DaysOfCode #ProblemSolving #CodingChallenge #DataStructuresAndAlgorithms #ProgrammingJourney #TechLearning #AlgorithmDesign
To view or add a comment, sign in
-
-
🚀 Turning Code into Strategy Just wrapped up a fun exercise in algorithmic thinking: finding the best day to buy and sell for maximum profit using Java! This snippet dives into a classic problem—optimizing transactions based on price fluctuations. 📈 Input: {3, 1, 4, 0, 7, 5} 💡 Output: Maximum Profit: 8 | Buy Day: 1 | Sell Day: 7 It’s a great reminder that behind every smart decision is a smart algorithm. #Java #ProblemSolving #Algorithms #CodingJourney #TechInsights #LinkedInLearning
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Winter may be coming ❄️, but the code still runs fast ⚔️ Cracked one of the toughest LeetCode Hard problems – Median of Two Sorted Arrays 🔥 Hit 100% runtime efficiency (1 ms) with clean and optimized binary search logic 🧠 This one tested everything — edge cases, math logic, and patience 😅 But as they say… “You win or you debug again.” 🐉 #LeetCode #DSA #CodingJourney #BinarySearch #Java #ProblemSolving #WinterIsComing #GameOfCodes
To view or add a comment, sign in
-
-
Day 41 of #100DaysOfCode Problem: 3228. Maximum Number of Operations to Move Ones to the End Difficulty: Medium Language: Java Status: Solved Problem Summary: Given a binary string s, you can repeatedly choose an index i such that: s[i] == '1' and s[i + 1] == '0', and move that '1' to the right until it either reaches the end of the string or sits just before another '1'. You must find the maximum number of such operations possible. Key Idea: Every '0' that appears after a '1' contributes to new operations. Each '0' can “absorb” all previous '1's — since each of them can eventually be moved right past it. Hence, for every '0' that immediately follows a '1', we can add the total count of '1's so far to our result. Algorithm Steps: Initialize counters: ones = 0, res = 0. Traverse the string: When you see '1', increment ones. When you see '0' after a '1', add ones to res. Return res. Time Complexity: O(n) Space Complexity: O(1) Learning Takeaway: Great example of prefix accumulation logic — counting how many prior elements influence the current one. Efficiently transforms a greedy movement problem into a simple linear counting one. #Day41 #100DaysOfCode #LeetCode #StringManipulation #Greedy #Java #Algorithms #CodingChallenge #ProblemSolving #DSA
To view or add a comment, sign in
-
Explore related topics
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