Median of Two Sorted Arrays

Given two sorted arrays A and B of sizes N and M, find the median of the merged array formed by combining arrays A and B. If the total number of elements, N + M, is even, the median will be the mean of the two middle elements.

Input:

The first line of input contains an integer 'T' representing the number of test cases.
Each test case contains:
- Two space-separated integers 'N' and 'M' denoting the sizes of arrays 'A' and 'B'.
- A line with 'N' space-separated integers representing the elements of array 'A'.
- A line with 'M' space-separated integers representing the elements of array 'B'.

Output:

For each test case, output a single line with a number representing the median of the combined array. Each test case output should be on a new line.

Example:

Input:
A = {2, 4, 6, 8} and B = {1, 3, 5, 7}.
The merged array is {1, 2, 3, 4, 5, 6, 7, 8}.
Since the total number of elements is even, the medians are 4 and 5.
Thus, the median is the mean of 4 and 5, which is 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 do not need to print anything manually; just implement the function to return the result.
AnswerBot
4mo

Find the median of two sorted arrays by merging them and calculating the middle element(s).

  • Merge the two sorted arrays into one sorted array.

  • Calculate the median based on the total number of elements ...read more

Help your peers!
Select
Add answer anonymously...

Nagarro Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Word Break Problem Statement You are given a list of N strings called A. Your ta...read more
A Software Developer Intern was asked Q. Sort 0 and 1 Problem Statement Given an integer array ARR of size N containing o...read more
A Software Developer Intern was asked Q. Check If Binary Representation of a Number is Palindrome Given an integer N, det...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Word Break Problem Statement You are given a list of N strings called A. Your ta...read more
A Software Developer Intern was asked Q2. Sort 0 and 1 Problem Statement Given an integer array ARR of size N containing o...read more
A Software Developer Intern was asked Q3. Check If Binary Representation of a Number is Palindrome Given an integer N, det...read more
Nagarro 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