## 4. Median of Two Sorted Arrays

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`

## Solution:-

``````class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
if (m > n) {
int[] temp = nums1; nums1 = nums2; nums2 = temp;
int tmp = m; m = n; n = tmp;
}
int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
while (iMin <= iMax) {
int i = (iMin + iMax) / 2;
int j = halfLen - i;
if (i < iMax && nums2[j-1] > nums1[i]){
iMin = i + 1;
}
else if (i > iMin && nums1[i-1] > nums2[j]) {
iMax = i - 1;
}
else {
int maxLeft = 0;
if (i == 0) { maxLeft = nums2[j-1]; }
else if (j == 0) { maxLeft = nums1[i-1]; }
else { maxLeft = Math.max(nums1[i-1], nums2[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; }

int minRight = 0;
if (i == m) { minRight = nums2[j]; }
else if (j == n) { minRight = nums1[i]; }
else { minRight = Math.min(nums2[j], nums1[i]); }

return (maxLeft + minRight) / 2.0;
}
}
return 0.0;

}
}``````

Visit For More :- https://softwaretechit.com
HackerRank Problems Solution :- https://softwaretechit.com/category/hackerrank
LeetCode Problems Solution:- https://softwaretechit.com/category/leetcode
Blog:- https://blog.softwaretechit.com
Shop:- https://shop.softwaretechit.com

Quetions :-
What is the median of the two sorted arrays?
How do you find the median of two sorted arrays in C++?
How do you find the median of two sorted arrays in Java?
What is median of sorted array C++?
How to find median of two arrays in C?
How do you find the median between two arrays?
How do you find the median if there are 2 medians?
What are the two formulas of median?
How do you find the median if there is 2?
What is the median of 2 and 6?
Do you always divide the median by 2?
What is the median of the following 2 3 5 7 9?
What is the median of 2 4 6 8 10 12?
What is the median of 1 to 10?
What is the median of 1 to 50?
What is the median of 3 11 7 2 5 9 9 2 10?
What is the median of 7 and 4?
What is the median of 15 6 16 8 22 21 9 18 25?
What is the mean of the following numbers 2 6 4 9 7 3 5 4 3 7?
What is the median if mode is 15 and 48?
What is the mode of 23 15 25 40 27 25 22 25 20?

leetcode,java,leetcode java,leetcode problems,leetcode solutions,problems,leetcode problems java,leetcode java solutions,leetcode 1,leetcode problem solving java,problem solving,two sum leetcode,leetcode easy problems,leetcode problems solve,leetcode solved problems,leetcode google problems,how to use leetcode effectively,solving leetcode problems,leetcode two sum,leetcode preparation,leetcode beginner problems,java leetcode #softwaretechit #2023 #tech