🚀 Day 21 of #100DaysOfCode 🧩 Problem: Find Median of Two Sorted Arrays (LeetCode #4, Hard) 💡 Concept: Given two sorted arrays, the goal is to find the median of the combined dataset after merging both arrays. 🛠 Approach (Merge + Sort): 1️⃣ Create a new array to store elements from both input arrays. 2️⃣ Copy all elements from nums1 and nums2 into the new array. 3️⃣ Sort the merged array using Bubble Sort to maintain order. 4️⃣ Determine the median: If total length is even, return the average of the two middle elements. If odd, return the middle element directly. 🔥 Performance: ✅ Accepted on LeetCode ⚡ Runtime: 52 ms 💾 Memory: 48.81 MB 📊 Complexity: ⏱ Time: O((m + n)²) — due to bubble sort 💾 Space: O(m + n) — extra array used for merging #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Consistency #LearningInPublic #CodingJourney
Finding Median of Two Sorted Arrays with LeetCode Solution
More Relevant Posts
-
🚀 Day 76 of #100DaysOfCode Today I worked on a clean and elegant array manipulation problem — LeetCode: Rotate Array ✅ 📌 Problem Summary Given an array, rotate it to the right by k steps in-place, without using extra space. 🧠 Approach Used: Reverse Technique Instead of shifting elements one by one, I used a smart 3-step reverse strategy: Reverse the entire array Reverse the first k elements Reverse the remaining n − k elements This achieves the rotation efficiently and keeps the solution simple. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) (in-place) 🔥 Key Learnings Sometimes the best solution is about reordering operations, not adding complexity In-place algorithms are powerful when space constraints matter Reverse logic is a recurring and underrated pattern in array problems Onward to Day 77 🚀 Consistency > Motivation 💪 #100DaysOfCode #LeetCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode – Day 12 Solved Search in Rotated Sorted Array II on LeetCode 🔄🔍 🧠 Key insight: When duplicates are present, it’s not always possible to directly identify the sorted half. If nums[left] == nums[mid], we can safely move left++ to reduce the search space and continue. ⚙️ Approach: 🔹Use modified binary search 🔹Handle duplicates by skipping equal boundary elements 🔹Identify the sorted half 🔹Check if the target lies in that range and adjust pointers accordingly ⏱️ Time Complexity: Average: O(log n) Worst case (many duplicates): O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 9 of #100DaysOfCode Solved Search in Rotated Sorted Array II on LeetCode using Modified Binary Search (Handling Duplicates) 🔎 🧠 Key insight: With duplicates present, it’s sometimes impossible to identify the sorted half directly. If nums[left] == nums[mid], we safely move left++ to shrink the search space. ⚙️ Approach: 🔹Apply binary search 🔹Handle duplicates by skipping equal boundary values 🔹Identify sorted half and check if target lies within it 🔹Continue search accordingly ⏱️ Time Complexity: O(log n) average, O(n) worst case (due to duplicates) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 4/100 – LeetCode Challenge Problem Solved: Find First and Last Position of Element in Sorted Array (34) Today I worked on a problem that requires finding the starting and ending index of a target value in a sorted array. If the target is not present, the result should be [-1, -1]. The important constraint is that the solution must run in O(log n) time. Since the array is sorted, this naturally points to Binary Search. Instead of performing a simple search, the idea is to perform two modified binary searches: One to find the first occurrence of the target, and another to find the last occurrence. By carefully adjusting the search boundaries when the target is found, we can narrow down the exact range. This ensures: The algorithm maintains logarithmic time complexity. The solution handles duplicates correctly. Edge cases such as the target not being present are properly addressed. Time Complexity: O(log n) Space Complexity: O(1) Key takeaway: Binary search is not just about finding whether an element exists. With small boundary modifications, it can be adapted to solve range-based problems efficiently. Understanding how to tweak the search condition is more important than memorizing the template. Day 4 completed. Continuing to sharpen problem-solving fundamentals. #100DaysOfLeetCode #BinarySearch #Java #DataStructures #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 11 of #100DaysOfCode Solved Find Minimum in Rotated Sorted Array on LeetCode using Binary Search 🔄 🧠 Key insight: In a rotated sorted array, at least one half is always sorted. By comparing the middle element with the right boundary, we can determine which half contains the minimum and safely discard the other. ⚙️ Approach: 🔹Use binary search on the array 🔹Compare nums[mid] with nums[right] 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹Else → minimum lies in the left half (including mid) ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 27/100 – LeetCode Challenge 🚀 Problem: Sort List Approach: Applied Merge Sort on the linked list Used slow and fast pointers to find the middle Recursively sorted both halves Merged the sorted halves Time Complexity: O(n log n) Space Complexity: O(log n) (recursion stack) Key takeaway: Merge sort is the most efficient sorting technique for linked lists, as it avoids random-access operations required by other algorithms. #LeetCode #100DaysOfCode #DSA #Java #LinkedList #MergeSort #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 6 of #100DaysOfCode Solved Find the Duplicate Number on LeetCode using an in-place cyclic sort approach 🔁 🧠 Key insight: When numbers are in the range 1…n, each value belongs at index value − 1. If during placement we find that a number is already at its correct position, that number must be the duplicate. ⚙️ Approach: 🔹Iterate through the array 🔹Place each number at its correct index (num − 1) using swaps 🔹If a conflict occurs (same number already present), return it ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (no extra data structures) #100DaysOfCode #LeetCode #DSA #CyclicSort #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🌳 Day 59/100: Preorder Traversal - Root, Left, Right Day 59. Learning tree traversals. Preorder: Visit root first, then explore. 🎯 📌 Problem: Return preorder traversal of binary tree. 🎯 Recursion: Base case: If node is null, return. Recursive case: Add value, go left, go right. Simple and clean. 📊 Complexity: O(n) time, O(h) space (recursion stack) 🌱 Tree Traversals: Preorder: Root → Left → Right Inorder: Left → Root → Right Postorder: Left → Right → Root Day 59. Traversal patterns building. 🌳 #100DaysOfCode #DSA #LeetCode #Day59 #Java #PreorderTraversal #BinaryTree #TreeTraversal
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge 🚀 Problem: Remove Element Approach: Used two pointers Shifted non-matching elements to the front Returned the count of valid elements Time Complexity: O(n) Space Complexity: O(1) (in-place) Key takeaway: In-place array problems often reduce to index management, not extra data structures. #LeetCode #100DaysOfCode #DSA #Java #ProblemSolving #InterviewPrep #100DaysOfLeetCode
To view or add a comment, sign in
-
-
Squares of a Sorted Array Solved “Squares of a Sorted Array” using a two-pointer technique instead of brute force. Key idea: Since the array is already sorted, the largest square will always come from either end. Use two pointers (start, end) and fill the result array from back to front. Why this approach? Avoids sorting after squaring Optimized and clean logic Complexity: Time: O(n) Space: O(n) Takeaway: Understanding patterns like two pointers helps convert naive solutions into optimized ones. #DSA #TwoPointers #Java #LeetCode #ProblemSolving
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