# Median of two sorted arrays of different sizes | Set 1 (Linear)

This is an extension of the median of two sorted arrays of equal size problem. Here we handle arrays of unequal size also.

Examples:

```Input : a[] ={1, 12, 15, 26, 38}
b[] = {2, 13, 24}
Output : 14
Explanation :
After merging arrays the result is
1, 2, 12, 13, 15, 24, 26, 38
median = (13+15)/2 = 14.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The approach discussed in this post is similar to method 1 of equal size median. Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count becomes ()(n1+n2)/2 (For n1+n2 elements), we have reached the median. Take the average of the elements at indexes ((n1+n2)/2)-1 and (n1+n2)/2 in the merged array. See the below implementation.

## C++

 `// A C++ program to find median of two sorted arrays of ` `// unequal sizes ` `#include ` `using` `namespace` `std; ` ` `  `// A utility function to find median of two integers ` `/* This function returns median of a[] and b[]. ` `Assumptions in this function: Both a[] and b[] ` `are sorted arrays  */` `float` `findmedian(``int` `a[], ``int` `n1, ``int` `b[], ``int` `n2) ` `{ ` `    ``int` `i = 0; ``/* Current index of ` `             ``i/p array a[] */` `    ``int` `j = 0; ``/* Current index of ` `                  ``i/p array b[] */` `    ``int` `k; ` `    ``int` `m1 = -1, m2 = -1; ` `    ``for` `(k = 0; k <= (n1 + n2) / 2; k++) { ` ` `  `        ``if` `(i < n1 && j < n2) { ` `            ``if` `(a[i] < b[j]) { ` `                ``m2 = m1; ` `                ``m1 = a[i]; ` `                ``i++; ` `            ``} ` `            ``else` `{ ` `                ``m2 = m1; ` `                ``m1 = b[j]; ` `                ``j++; ` `            ``} ` `        ``} ` ` `  `        ``/* Below is to handle the case where ` `           ``all elements of a[] are ` `           ``smaller than smallest(or first) ` `           ``element of b[] or a[] is empty*/` `        ``else` `if` `(i == n1) { ` `            ``m2 = m1; ` `            ``m1 = b[j]; ` `            ``j++; ` `        ``} ` ` `  `        ``/* Below is to handle case where ` `           ``all elements of b[] are ` `           ``smaller than smallest(or first) ` `           ``element of a[] or b[] is empty*/` `        ``else` `if` `(j == n2) { ` `            ``m2 = m1; ` `            ``m1 = a[i]; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``/*Below is to handle the case where ` `     ``sum of number of elements  ` `     ``of the arrays is even */` `    ``if` `((n1 + n2) % 2 == 0) ` `        ``return` `(m1 + m2) * 1.0 / 2; ` ` `  `    ``/* Below is to handle the case where ` `       ``sum of number of elements  ` `       ``of the arrays is odd */` `    ``return` `m1; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 12, 15, 26, 38 }; ` `    ``int` `b[] = { 2, 13, 24 }; ` ` `  `    ``int` `n1 = ``sizeof``(a) / ``sizeof``(a); ` `    ``int` `n2 = ``sizeof``(b) / ``sizeof``(b); ` ` `  `    ``printf``(``"%f"``, findmedian(a, n1, b, n2)); ` ` `  `    ``return` `0; ` `} `

## Java

 `// A Java program to find median of two sorted arrays of ` `// unequal sizes ` ` `  `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  ` `  ` `  `// A utility function to find median of two integers ` `/* This function returns median of a[] and b[]. ` `Assumptions in this function: Both a[] and b[] ` `are sorted arrays */` `static` `float` `findmedian(``int` `a[], ``int` `n1, ``int` `b[], ``int` `n2) ` `{ ` `    ``int` `i = ``0``; ``/* Current index of ` `            ``i/p array a[] */` `    ``int` `j = ``0``; ``/* Current index of ` `                ``i/p array b[] */` `    ``int` `k; ` `    ``int` `m1 = -``1``, m2 = -``1``; ` `    ``for` `(k = ``0``; k <= (n1 + n2) / ``2``; k++) ` `    ``{ ` ` `  `        ``if` `(i < n1 && j < n2) ` `        ``{ ` `            ``if` `(a[i] < b[j]) ` `            ``{ ` `                ``m2 = m1; ` `                ``m1 = a[i]; ` `                ``i++; ` `            ``} ` `            ``else`  `            ``{ ` `                ``m2 = m1; ` `                ``m1 = b[j]; ` `                ``j++; ` `            ``} ` `        ``} ` ` `  `        ``/* Below is to handle the case where ` `        ``all elements of a[] are ` `        ``smaller than smallest(or first) ` `        ``element of b[] or a[] is empty*/` `        ``else` `if` `(i == n1)  ` `        ``{ ` `            ``m2 = m1; ` `            ``m1 = b[j]; ` `            ``j++; ` `        ``} ` ` `  `        ``/* Below is to handle case where ` `        ``all elements of b[] are ` `        ``smaller than smallest(or first) ` `        ``element of a[] or b[] is empty*/` `        ``else` `if` `(j == n2)  ` `        ``{ ` `            ``m2 = m1; ` `            ``m1 = a[i]; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``/*Below is to handle the case where ` `    ``sum of number of elements  ` `    ``of the arrays is even */` `    ``if` `((n1 + n2) % ``2` `== ``0``) ` `    ``{ ` `        ``return` `(m1 + m2) *(``float``) ``1.0` `/ ``2``; ` `    ``} ` `    ``/* Below is to handle the case where ` `    ``sum of number of elements  ` `    ``of the arrays is odd */` `    ``return` `m1; ` `} ` ` `  `// Driver program to test above functions ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `       ``int` `a[] = {``1``, ``12``, ``15``, ``26``, ``38` `}; ` `       ``int` `b[] = {``2``, ``13``, ``24``}; ` ` `  `       ``int` `n1 = a.length; ` `       ``int` `n2 = b.length; ` ` `  `       ``System.out.println( findmedian(a, n1, b, n2)); ` `    ``} ` `} ` ` `  `// This code has been contributed by inder_verma. `

## Python 3

 `# Python 3 program to find median of  ` `# two sorted arrays of unequal sizes ` ` `  `# A utility function to find median  ` `# of two integers ` `''' This function returns median of  ` `a[] and b[]. Assumptions in this  ` `function: Both a[] and b[] are sorted ` `arrays '''` ` `  `def` `findmedian(a, n1, b, n2): ` ` `  `    ``i ``=` `0` `# Current index of i/p array a[]  ` `    ``j ``=` `0` `# Current index of i/p array b[]  ` ` `  `    ``m1 ``=` `-``1` `    ``m2 ``=` `-``1` `    ``for` `k ``in` `range``(((n1 ``+` `n2) ``/``/` `2``) ``+` `1``) : ` ` `  `        ``if` `(i < n1 ``and` `j < n2) : ` `            ``if` `(a[i] < b[j]) : ` `                ``m2 ``=` `m1 ` `                ``m1 ``=` `a[i] ` `                ``i ``+``=` `1` `             `  `            ``else` `: ` `                ``m2 ``=` `m1 ` `                ``m1 ``=` `b[j] ` `                ``j ``+``=` `1` ` `  `        ``# Below is to handle the case where ` `        ``# all elements of a[] are ` `        ``# smaller than smallest(or first) ` `        ``# element of b[] or a[] is empty ` `             `  `        ``elif``(i ``=``=` `n1) : ` `            ``m2 ``=` `m1 ` `            ``m1 ``=` `b[j] ` `            ``j ``+``=` `1` `             `  `        ``# Below is to handle case where ` `        ``# all elements of b[] are ` `        ``# smaller than smallest(or first) ` `        ``# element of a[] or b[] is empty  ` `        ``elif` `(j ``=``=` `n2) : ` `            ``m2 ``=` `m1 ` `            ``m1 ``=` `a[i] ` `            ``i ``+``=` `1` ` `  `    ``'''Below is to handle the case  ` `    ``where sum of number of elements  ` `    ``of the arrays is even '''` `    ``if` `((n1 ``+` `n2) ``%` `2` `=``=` `0``): ` `        ``return` `(m1 ``+` `m2) ``*` `1.0` `/` `2` ` `  `    ``''' Below is to handle the case  ` `    ``where sum of number of elements  ` `    ``of the arrays is odd '''` `    ``return` `m1 ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``a ``=` `[ ``1``, ``12``, ``15``, ``26``, ``38` `] ` `    ``b ``=` `[ ``2``, ``13``, ``24` `] ` ` `  `    ``n1 ``=` `len``(a) ` `    ``n2 ``=` `len``(b) ` ` `  `    ``print``(findmedian(a, n1, b, n2)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// A C# program to find median ` `// of two sorted arrays of ` `// unequal sizes ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `// A utility function to find  ` `// median of two integers ` `/* This function returns  ` `   ``median of a[] and b[]. ` `   ``Assumptions in this  ` `   ``function: Both a[] and b[] ` `   ``are sorted arrays */` `static` `float` `findmedian(``int` `[]a, ``int` `n1, ` `                        ``int` `[]b, ``int` `n2) ` `{ ` `int` `i = 0; ``/* Current index of ` `              ``i/p array a[] */` `int` `j = 0; ``/* Current index of ` `              ``i/p array b[] */` `int` `k; ` `int` `m1 = -1, m2 = -1; ` `for` `(k = 0; k <= (n1 + n2) / 2; k++) ` `{ ` `    ``if` `(i < n1 && j < n2) ` `    ``{ ` `        ``if` `(a[i] < b[j]) ` `        ``{ ` `            ``m2 = m1; ` `            ``m1 = a[i]; ` `            ``i++; ` `        ``} ` `        ``else` `        ``{ ` `            ``m2 = m1; ` `            ``m1 = b[j]; ` `            ``j++; ` `        ``} ` `    ``} ` ` `  `    ``/* Below is to handle the case where ` `    ``all elements of a[] are ` `    ``smaller than smallest(or first) ` `    ``element of b[] or a[] is empty*/` `    ``else` `if` `(i == n1)  ` `    ``{ ` `        ``m2 = m1; ` `        ``m1 = b[j]; ` `        ``j++; ` `    ``} ` ` `  `    ``/* Below is to handle case where ` `    ``all elements of b[] are ` `    ``smaller than smallest(or first) ` `    ``element of a[] or b[] is empty*/` `    ``else` `if` `(j == n2)  ` `    ``{ ` `        ``m2 = m1; ` `        ``m1 = a[i]; ` `        ``i++; ` `    ``} ` `} ` ` `  `/*Below is to handle the case where ` `sum of number of elements  ` `of the arrays is even */` `if` `((n1 + n2) % 2 == 0) ` `{ ` `    ``return` `(m1 + m2) * (``float``) 1.0 / 2; ` `} ` `/* Below is to handle the case  ` `where sum of number of elements  ` `of the arrays is odd */` `return` `m1; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main () ` `{ ` `    ``int` `[]a = {1, 12, 15, 26, 38 }; ` `    ``int` `[]b = {2, 13, 24}; ` `     `  `    ``int` `n1 = a.Length; ` `    ``int` `n2 = b.Length; ` `     `  `    ``Console.WriteLine(findmedian(a, n1, b, n2)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Subhadeep Gupta `

## PHP

 ` `

Output:

```14.000000
```

Time Complexity: O(n)

More Efficient (Log time complexity methods)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.