🚀 Day 32 of #100DaysOfCode Solved 4. Median of Two Sorted Arrays on LeetCode 📊 🧠 Key insight: Since both arrays are already sorted, we can merge them into one sorted array and directly compute the median. ⚙️ Approach: 🔹Merge the two sorted arrays into a single sorted array 🔹Find the middle index of the merged array 🔹If the total length is: 🔹Odd → median is the middle element 🔹Even → median is the average of the two middle elements ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(m + n) #100DaysOfCode #LeetCode #DSA #Arrays #MergeSort #Java #ProblemSolving #InterviewPrep #LearningInPublic
Median of Two Sorted Arrays LeetCode Solution
More Relevant Posts
-
🚀 Day 41 of #100DaysOfCode Solved 189. Rotate Array on LeetCode 🔄 🧠 Key Insight: Rotating an array by k steps to the right means the last k elements move to the front, while the remaining elements shift to the right. ⚙️ Approach: 1️⃣ Compute effective rotations using k % n 2️⃣ Store the last k elements in a temporary array 3️⃣ Shift the remaining n-k elements to the right 4️⃣ Copy the stored elements back to the beginning of the array This ensures the rotation happens in-place with controlled extra space. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) #100DaysOfCode #LeetCode #DSA #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 16/100 – LeetCode Challenge Problem: Merge Sorted Array Today’s problem involved merging two sorted arrays into one sorted array. Approach: Created a temporary array of size m + n Used two pointers to compare elements from both arrays Inserted the smaller element into the new array Copied remaining elements if any array still had values Finally copied the merged result back into nums1 Complexity: Time: O(m + n) Space: O(m + n) Concepts Practiced: Two-pointer technique Array traversal Merging sorted arrays #100DaysOfCode #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 47 of #100DaysOfCode 🌱 Topic: Arrays / HashMap ✅ Problem Solved: LeetCode 260 – Single Number III 🛠 Approach: Used a HashMap to track frequency of elements. Traversed the array and stored counts of each number. Iterated through the map to find elements with frequency 1. Stored those elements in the result array. This approach is straightforward but uses extra space. #100DaysOfCode #Day47 #DSA #Arrays #HashMap #BitManipulation #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 34 of #100DaysOfCode Solved 852. Peak Index in a Mountain Array on LeetCode ⛰️📈 🧠 Key Insight: A mountain array strictly increases to a peak and then decreases. Instead of scanning the whole array, we can use Binary Search to efficiently find the peak. ⚙️ Approach: 🔹Use binary search with two pointers left and right 🔹Compare arr[mid] with arr[mid + 1] 🔹If arr[mid] > arr[mid + 1] → peak is on the left side (including mid) 🔹Otherwise → peak is on the right side 🔹Continue until left == right, which gives the peak index ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 DSA Consistency - Day 57 Today I solved the Same Tree problem on LeetCode, which focuses on understanding binary tree structure comparison using recursion. The goal is to determine whether two binary trees are structurally identical and have the same node values. 🧠 Approach: Recursive Tree Traversal To check if two trees are the same: 1️⃣ If both nodes are null, they are identical → return true. 2️⃣ If one node is null and the other is not, trees differ → return false. 3️⃣ If node values are different, trees are not identical. 4️⃣ Recursively check: Left subtree of both trees Right subtree of both trees Both must match for the trees to be identical. ⏱ Complexity Analysis Time Complexity: O(n) Each node is visited once. Space Complexity: O(h) Due to recursion stack (where h is the height of the tree). #DSA #LeetCode #BinaryTree #Java #CodingJourney #Consistency #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 | Median of Two Sorted Arrays Intuition: We are given two sorted arrays and need to find the median of the combined numbers. Since both arrays are already sorted, we can merge them in sorted order. Once we have the merged array, finding the median becomes simple. If the total number of elements is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. Approach: Use two pointers to traverse both arrays. Compare the elements and insert the smaller one into a new array. Continue this process until all elements are merged. Finally, calculate the median based on the length of the merged array. Complexity: Time Complexity: O(n + m) Space Complexity: O(n + m) #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
#day332 of #1001daysofcode problem statement (0226): Invert Binary Tree The idea is to recursively swap the left and right child of every node in the tree. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of tree) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 56 - Search a 2D Matrix Approached the problem by leveraging the sorted property of the matrix. Applied binary search over virtual 1D space instead of traversing row-wise. Time Complexity: O(log(m*n)) #Day56 #LeetCode #Java #BinarySearch #Matrix #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
Day 35 – Plus One Worked on a problem where a large integer is represented as an array of digits, and the task was to increment the number by one. Key Learnings: Traversing arrays from right to left to simulate digit addition Handling carry when a digit becomes 10 Managing edge cases where all digits are 9 by creating a new array #DSA #Java #Arrays #ProblemSolving #CodingPractice #LeetCode
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