Day 3 — Median of Two Sorted Arrays Today’s focus was on one of the most popular LeetCode problems : "Median of Two Sorted Arrays.” The goal: merge two sorted arrays and find their median. At first glance, it feels simple — but getting it right with edge cases is where the logic truly kicks in. Used a two-pointer merge approach — merging elements in order until both arrays are traversed. Once the merged array is ready, finding the median becomes straightforward. It’s one of those problems that looks basic but builds a strong foundation for array manipulation and binary search-based optimizations later on. Slow progress, steady learning.. Similar problems: 🔹 Merge Sorted Array — LeetCode #88 🔹 Kth Element of Two Sorted Arrays (variation of this problem, often asked in interviews) 🔹 Find the Median from Data Stream — LeetCode #295 #100DaysOfDSA #LeetCode #Java #Arrays #ProblemSolving
Solving Median of Two Sorted Arrays on LeetCode
More Relevant Posts
-
📌 LeetCode Day 60 — #106. Construct Binary Tree from Inorder and Postorder Traversal Problem Description: You are given two integer arrays inorder and postorder representing the inorder and postorder traversal of a binary tree. Your task is to reconstruct the original binary tree from these traversals. Approach: Identify Root: In postorder, the last element is always the root. Find Root Index in Inorder: Using a HashMap, find the position of the root in the inorder array to separate left and right subtrees. Recursive Construction: Build the left subtree from the left part of inorder and corresponding postorder range. Build the right subtree similarly from the right parts. Return Root: Recursively combine subtrees to reconstruct the full tree. Complexity Analysis: Time Complexity: O(n) — each node is processed once using the HashMap lookup. Space Complexity: O(n) — recursion stack + map for index lookup. Hashtags: #BinaryTree #Recursion #HashMap #LogicBuilding #LeetCode100Days #Day60 #Java #ProblemSolving
To view or add a comment, sign in
-
-
✨ Day 52 of 100: Copy List with Random Pointer ✨ Today’s challenge was LeetCode 138 – Copy List with Random Pointer 🧠 The task: Given a linked list where each node has two pointers — 🔹 next: points to the next node 🔹 random: points to any node in the list (or null) we need to create a deep copy of this list. 💡 Key Takeaways: 🔹 Learned how to handle complex data structures where nodes reference each other in non-linear ways. 🔹 Practiced creating a deep copy using HashMap to maintain the mapping between original and copied nodes. 🔹 Explored an optimized in-place approach — interleaving original and copied nodes to avoid extra space. 🔹 Strengthened understanding of pointer manipulation and object references in Java. 🚀 This problem deepened my appreciation for how data structure cloning works — not just duplicating values, but preserving relationships! #LeetCode #100DaysOfCode #Day52 #Java #LinkedList #DeepCopy #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
#Day_23 💡 Problem Solved: Intersection of Two Arrays Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
To view or add a comment, sign in
-
-
#Day_24 💡 Problem Solved: Intersection of Two Arrays II Today’s challenge tested my ability to handle array manipulation and two-pointer logic efficiently. 🧩 Problem Overview: Given two integer arrays, the task is to find their intersection — meaning the common elements between both arrays, including duplicates. For example: nums1 = [4,9,5] and nums2 = [9,4,9,8,4] 👉 Output should be [4,9] (since both 4 and 9 appear in both arrays). ⚙️ My Approach: Sorting both arrays: By sorting nums1 and nums2, I was able to efficiently compare elements in a linear scan. Two-pointer technique: I used two indices i and j, starting from 0 in each array. If both elements matched → it’s part of the intersection. If one was smaller → move that pointer ahead to catch up. Storing results dynamically: Added all matching elements into an ArrayList. Finally converted it into an integer array for the result. This approach ensures O(n log n) time complexity due to sorting, but O(n) in space — a solid balance between clarity and performance. 🧠 Key Takeaways: Sorting simplifies complex comparison problems. The two-pointer pattern is extremely useful for dealing with sorted arrays. Always remember: clean logic > brute force. 🔥 Every new problem brings a fresh perspective on optimizing logic and learning better ways to handle data structures. #Java #CodingChallenge #LeetCode #100DaysOfCode #ProblemSolving #DSA #ProgrammingJourney #TechWithMudassir
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
🌳 Day 73 of #100DaysOfCode 🌳 💡 Problem: Binary Tree Inorder Traversal – LeetCode ✨ Approach: Implemented a recursive inorder traversal to explore the binary tree in Left → Root → Right order. A perfect balance of recursion and logic — the beauty of tree traversal lies in its simplicity 🌿 📊 Complexity Analysis: Time Complexity: O(n)** — visiting each node exactly once Space Complexity: O(n)** — due to recursion stack and list storage ✅ Runtime: 0 ms (🔥 Beats 100%) ✅ Memory: 43.25 MB 🔑 Key Insight: Recursion simplifies complex tree structures into elegant solutions — one node at a time 🌱 #LeetCode #100DaysOfCode #DataStructures #BinaryTree #Recursion #ProblemSolving #DSA #AlgorithmDesign #CodeEveryday #ProgrammerJourney #Java #CodingChallenge
To view or add a comment, sign in
-
-
🔹 Day 36: Merge Sorted Array (LeetCode #88) 📌 Problem Statement: You are given two sorted arrays, nums1 and nums2, and two integers m and n representing the number of initialized elements in each. Merge nums2 into nums1 as one sorted array in non-decreasing order. ✅ My Approach: I appended all elements of nums2 into the extra space of nums1 and then sorted the combined array. While this is a simple approach, an optimized method could merge from the end to avoid extra sorting. 📊 Complexity: Time Complexity: O((m + n) log(m + n)) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 1 ms (Beats 27.92%) Memory: 42.32 MB 💡 Reflection: This problem reinforces the importance of understanding in-place array manipulation. A clean and straightforward implementation that still gets the job done! 💪 #LeetCode #Java #Arrays #Sorting #100DaysOfCode #Day36
To view or add a comment, sign in
-
-
🌟 Day 76 of #100DaysOfCodingChallenge 📘 Problem: LeetCode 73 – Set Matrix Zeroes Today, I explored an important array manipulation problem — setting entire rows and columns to zero if any element in a 2D matrix is zero. 🧩 Problem Understanding Given an m x n matrix, if any cell has the value 0, we must make its entire row and column 0. For example: Input: [ [1, 2, 3], [4, 0, 6], [7, 8, 9] ] Output: [ [1, 0, 3], [0, 0, 0], [7, 0, 9] ] 💡 My Approach (Brute Force) To handle this, I used two extra arrays — one to track rows and another to track columns that should become zero. 🪄 Steps followed: 1️⃣ Traverse the matrix and store the indices of rows and columns containing 0. 2️⃣ Loop through the matrix again and make the elements 0 wherever their row or column index is marked. This simple approach helps clearly visualize how matrix updates propagate. 🧠 Concept Learned: How to access and manipulate specific rows and columns in a 2D array. The importance of auxiliary data structures (like boolean arrays) in preserving information during traversal. 🕒 Complexity: Time: O(m × n) Space: O(m + n) 🧵 Next Step: I’ll be optimizing this approach to an O(1) space solution using the matrix itself as a marker — a neat trick in in-place algorithms! #100DaysOfCode #Day75 #LeetCode #Java #CodingChallenge #ProblemSolving #ArrayManipulation #DataStructures #WomenInTech
To view or add a comment, sign in
-
-
💻 Day 63 of #LeetCode100DaysChallenge Solved LeetCode 290: Word Pattern — a great problem for strengthening hash mapping and bijection logic between characters and words. 🧩 Problem: Given a pattern and a string s, determine if s follows the same pattern. Each letter in the pattern maps to exactly one unique word in s, and each unique word in s maps to exactly one letter in the pattern. No two letters map to the same word, and no two words map to the same letter. 💡 Approach — HashMap + Set: 1️⃣ Split s into words. If pattern length ≠ word count, return false. 2️⃣ Use two data structures: A HashMap<Character, String> to map pattern → word. A HashSet<String> to track if a word is already mapped. 3️⃣ Iterate through both: If the character was seen before, check if it maps to the same word. If it’s new, ensure the word isn’t already mapped to another letter. 4️⃣ Return true if all pairs match consistently. ⚙️ Complexity: Time: O(N) — one pass through all words. Space: O(N) — for the map and set. ✨ Key Takeaways: ✅ Strengthened understanding of bijections and hash-based pattern matching. ✅ Improved clarity on how to manage two-way mapping between characters and strings. ✅ Practiced clean iteration and map validation logic — crucial for real-world parsing and pattern-matching problems. #LeetCode #100DaysOfCode #Java #HashMap #PatternMatching #DSA #ProblemSolving #WomenInTech #CodingChallenge
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