LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Problems C++
Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for the Median of Two Sorted Arrays in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
About LeetCode
LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.
LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.
LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either Easy, Medium, or Hard.
LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:
- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.
Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.
Link for the Problem – Median of Two Sorted Arrays– LeetCode Problem
Median of Two Sorted Arrays– LeetCode Problem
Problem:
Given two sorted arrays nums1
and nums2
of size m
and n
respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n))
.
Example 1:
Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
Median of Two Sorted Arrays– LeetCode Solutions
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { const int n1 = nums1.size(); const int n2 = nums2.size(); if (n1 > n2) return findMedianSortedArrays(nums2, nums1); int l = 0; int r = n1; while (l <= r) { const int partition1 = l + (r - l) / 2; const int partition2 = (n1 + n2 + 1) / 2 - partition1; const int maxLeft1 = partition1 == 0 ? INT_MIN : nums1[partition1 - 1]; const int maxLeft2 = partition2 == 0 ? INT_MIN : nums2[partition2 - 1]; const int minRight1 = partition1 == n1 ? INT_MAX : nums1[partition1]; const int minRight2 = partition2 == n2 ? INT_MAX : nums2[partition2]; if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) return (n1 + n2) % 2 == 0 ? (max(maxLeft1, maxLeft2) + min(minRight1, minRight2)) * 0.5 : max(maxLeft1, maxLeft2); else if (maxLeft1 > minRight2) r = partition1 - 1; else l = partition1 + 1; } throw; } };
class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { final int n1 = nums1.length; final int n2 = nums2.length; if (n1 > n2) return findMedianSortedArrays(nums2, nums1); int l = 0; int r = n1; while (l <= r) { final int partition1 = l + (r - l) / 2; final int partition2 = (n1 + n2 + 1) / 2 - partition1; final int maxLeft1 = partition1 == 0 ? Integer.MIN_VALUE : nums1[partition1 - 1]; final int maxLeft2 = partition2 == 0 ? Integer.MIN_VALUE : nums2[partition2 - 1]; final int minRight1 = partition1 == n1 ? Integer.MAX_VALUE : nums1[partition1]; final int minRight2 = partition2 == n2 ? Integer.MAX_VALUE : nums2[partition2]; if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) return (n1 + n2) % 2 == 0 ? (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) * 0.5 : Math.max(maxLeft1, maxLeft2); else if (maxLeft1 > minRight2) r = partition1 - 1; else l = partition1 + 1; } throw new IllegalArgumentException(); } }
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: n1 = len(nums1) n2 = len(nums2) if n1 > n2: return self.findMedianSortedArrays(nums2, nums1) l = 0 r = n1 while l <= r: partition1 = l + (r - l) // 2 partition2 = (n1 + n2 + 1) // 2 - partition1 maxLeft1 = -2**31 if partition1 == 0 else nums1[partition1 - 1] maxLeft2 = -2**31 if partition2 == 0 else nums2[partition2 - 1] minRight1 = 2**31 - 1 if partition1 == n1 else nums1[partition1] minRight2 = 2**31 - 1 if partition2 == n2 else nums2[partition2] if maxLeft1 <= minRight2 and maxLeft2 <= minRight1: return (max(maxLeft1, maxLeft2) + min(minRight1, minRight2)) * 0.5 if (n1 + n2) % 2 == 0 else max(maxLeft1, maxLeft2) elif maxLeft1 > minRight2: r = partition1 - 1 else: l = partition1 + 1
free shipping cialis buy tadalafil 40mg generic buy generic ed pills
order duricef 500mg generic cefadroxil 250mg sale oral propecia 1mg
fluconazole 200mg sale ciprofloxacin 1000mg without prescription ciprofloxacin oral
buy flagyl 200mg online cheap buy cephalexin 250mg for sale buy keflex 125mg pill
cleocin 150mg oral buy fildena 100mg generic brand sildenafil 50mg
bimatoprost allergy nasal spray buy desyrel without a prescription order desyrel for sale
order suhagra 50mg pill aurogra 100mg for sale sildenafil for men
isotretinoin 20mg tablet buy zithromax tablets buy azithromycin 250mg sale
order azipro 500mg online neurontin 600mg for sale order gabapentin online cheap
lasix pill monodox pills generic antihistamine pills
buy vardenafil 20mg online buy zanaflex cheap plaquenil without prescription
buy generic altace 10mg order etoricoxib 60mg pills order etoricoxib 120mg sale
vardenafil online buy zanaflex for sale cheap plaquenil 200mg
order asacol 800mg sale order astelin 10ml for sale cost irbesartan
order benicar 10mg without prescription purchase calan depakote pills
coreg 6.25mg sale cenforce 100mg for sale aralen 250mg pills
acetazolamide over the counter imdur 40mg usa azathioprine 25mg tablet
lanoxin 250 mg drug purchase lanoxin generic order molnunat 200 mg pill
naprosyn oral order generic naprosyn 500mg order lansoprazole 15mg generic
where to buy olumiant without a prescription brand baricitinib lipitor order
buy proventil 100 mcg online buy proventil online oral phenazopyridine
how to buy montelukast avlosulfon 100 mg for sale avlosulfon 100mg tablet
buy generic norvasc for sale amlodipine 5mg sale generic prilosec 10mg
nifedipine uk order adalat pills cheap fexofenadine 180mg