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.
AnswerBot
4mo

Find the median of two sorted arrays after merging them.

  • Merge the two sorted arrays into one sorted array.

  • Calculate the median based on the length of the combined array.

  • Handle cases where the total nu...read more

Help your peers!
Select
Add answer anonymously...

LinkedIn Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Split the String Problem Statement You are given a string str consisting of N lo...read more
A Software Developer Intern was asked Q. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Software Developer Intern was asked Q. Check if Two Trees are Mirror Given two arbitrary binary trees, your task is to ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Split the String Problem Statement You are given a string str consisting of N lo...read more
A Software Developer Intern was asked Q2. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Software Developer Intern was asked Q3. Check if Two Trees are Mirror Given two arbitrary binary trees, your task is to ...read more
LinkedIn Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits