Median of Two Sorted Arrays Problem Statement

Given two sorted integer arrays A and B with sizes N and M respectively, find the median of the combined array that results from merging arrays A and B. If the total number of elements (i.e., N + M) is even, the median will be the average of the two middle numbers.

Input:

The first line contains an integer T representing the number of test cases.
For each test case:
- The first line contains two space-separated integers N and M denoting the sizes of arrays A and B, respectively.
- The second line contains N space-separated integers representing the elements of the array A.
- The third line contains M space-separated integers representing the elements of the array B.

Output:

For each test case, print the median of the combined array on a new line.

Example:

Input:
A = { 2, 4, 6, 8 }
B = { 1, 3, 5, 7 }
Output:
4.5
Explanation: The merged array is { 1, 2, 3, 4, 5, 6, 7, 8 }. The middle elements are 4 and 5, thus the median is (4 + 5) / 2 = 4.5.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 10^6
  • 1 ≤ M ≤ 10^6
  • 1 ≤ A[i] ≤ 10^9
  • 1 ≤ B[i] ≤ 10^9
  • Time limit: 1 sec.

Note:

You are not required to print anything; just implement the function to find the median.
Be the first one to answer
Add answer anonymously...
LinkedIn Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter