Given two sorted arrays of sizes **N** and **M** respectively. The task is to find the median of the two arrays when they get merged.

If there are even number of elements in the resulting array, find the floor of the average of two medians.

Example 1:

Input:N = 5, M = 6 arr[] = {1,2,3,4,5} brr[] = {3,4,5,6,7,8}Output:4

## Median of Two Sorted Arrays GFG Solution in Java:

//Median of Two Sorted Arrays GFG Solution in Java //User function Template for Java class Solution { //Function to find the median of the two arrays when they get merged. public static int findMedian(int arr[], int m, int brr[], int n) { // if(n>m) return findMedian(brr,m,arr,n); if(m>n) return findMedian(brr,n,arr,m); int low = 0, high = m; while(low <= high) { int i1 = (low + high) / 2; int i2 = ((m + n + 1) / 2 )- i1; int min1 = (i1 == m)?Integer.MAX_VALUE:arr[i1]; int max1 = (i1 == 0)?Integer.MIN_VALUE:arr[i1 - 1]; int min2 = (i2 == n)?Integer.MAX_VALUE:brr[i2]; int max2 = (i2 == 0)?Integer.MIN_VALUE:brr[i2 - 1]; if(max1 <= min2 && max2 <= min1) { if((m + n) % 2 == 0) return (Math.max(max1, max2) + Math.min(min1, min2)) / 2; else return Math.max(max1, max2); } else if(max1 > min2) high = i1 - 1; else low = i1 + 1; } return -1; } }

**Disclaimer:** This problem**(Median of Two Sorted Arrays)** is originally created by GeeksforGeeks. Codesagar only provides a solution for it. This solution is only for Educational and learning purposes.