There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)).

##
Recommended: Please solve it on “__PRACTICE__ ” first, before moving on to the solution.

__PRACTICE__**Note : **Since size of the set for which we are looking for median is even (2n), we need take average of middle two numbers and return floor of the average.

**Method 1 (Simply count while Merging)**

Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count becomes n(For 2n elements), we have reached the median. Take the average of the elements at indexes n-1 and n in the merged array. See the below implementation.

## C#

`// A Simple Merge based O(n) solution ` `// to find median of two sorted arrays ` `using` `System; ` `class` `GFG { ` ` ` `// function to calculate median ` ` ` `static` `int` `getMedian(` `int` `[] ar1, ` ` ` `int` `[] ar2, ` ` ` `int` `n) ` ` ` `{ ` ` ` `int` `i = 0; ` ` ` `int` `j = 0; ` ` ` `int` `count; ` ` ` `int` `m1 = -1, m2 = -1; ` ` ` ` ` `// Since there are 2n elements, ` ` ` `// median will be average of ` ` ` `// elements at index n-1 and n in ` ` ` `// the array obtained after ` ` ` `// merging ar1 and ar2 ` ` ` `for` `(count = 0; count <= n; count++) { ` ` ` `// Below is to handle case ` ` ` `// where all elements of ar1[] ` ` ` `// are smaller than smallest ` ` ` `// (or first) element of ar2[] ` ` ` `if` `(i == n) { ` ` ` `m1 = m2; ` ` ` `m2 = ar2[0]; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `/* Below is to handle case where all ` ` ` `elements of ar2[] are smaller than ` ` ` `smallest(or first) element of ar1[] */` ` ` `else` `if` `(j == n) { ` ` ` `m1 = m2; ` ` ` `m2 = ar1[0]; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `if` `(ar1[i] < ar2[j]) { ` ` ` `// Store the prev median ` ` ` `m1 = m2; ` ` ` `m2 = ar1[i]; ` ` ` `i++; ` ` ` `} ` ` ` `else` `{ ` ` ` `// Store the prev median ` ` ` `m1 = m2; ` ` ` `m2 = ar2[j]; ` ` ` `j++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `(m1 + m2) / 2; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] ar1 = { 1, 12, 15, 26, 38 }; ` ` ` `int` `[] ar2 = { 2, 13, 17, 30, 45 }; ` ` ` ` ` `int` `n1 = ar1.Length; ` ` ` `int` `n2 = ar2.Length; ` ` ` `if` `(n1 == n2) ` ` ` `Console.Write(` `"Median is "` `+ getMedian(ar1, ar2, n1)); ` ` ` `else` ` ` `Console.Write(` `"arrays are of unequal size"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Median is 16

**
Method 2 (By comparing the medians of two arrays)**: This method works by first getting medians of the two sorted arrays and then comparing them.

Please refer complete article on Median of two sorted arrays of same size for more details!

## Recommended Posts:

- C# Program to Find the closest pair from two sorted arrays
- C# Program for Maximum size square sub-matrix with all 1s
- C# Program to find whether a no is power of two
- C# Program for Naive algorithm for Pattern Searching
- C# Program for KMP Algorithm for Pattern Searching
- C# Program to print all permutations of a given string
- C# Program for Count Inversions in an array | Set 1 (Using Merge Sort)
- C# Program for Activity Selection Problem | Greedy Algo-1
- C# Program for Subset Sum Problem | DP-25
- C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- C# Program for Maximum sum rectangle in a 2D matrix | DP-27
- C# Program for Largest Sum Contiguous Subarray
- C# Program for Minimum number of jumps to reach end
- C# Program for Cutting a Rod | DP-13
- C# Program for Longest Palindromic Subsequence | DP-12
- C# Program for Egg Dropping Puzzle | DP-11
- C# Program for Matrix Chain Multiplication | DP-8
- C# Program for Count set bits in an integer
- C# Program to Find the Number Occurring Odd Number of Times
- C# Program for Anagram Substring Search (Or Search for all permutations)