Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Structures and Algorithms

Median of Two Sorted Arrays (C++, Java, Python)

Piyush Kaushal by Piyush Kaushal
November 17, 2023
Reading Time: 9 mins read
Median of Two Sorted Arrays
Follow us on Google News   Subscribe to our newsletter

The problem of finding the median of two sorted arrays is a classic algorithmic challenge that has intrigued computer scientists, mathematicians, and programmers for years. In this article, we will solve the leetcode problem of How to Find the Median of Two Sorted Arrays using two different approaches.

What is the Median of a Sorted Array?

The median of an array is referred to as the center element of the sorted array. There can be two cases of finding it:

  1. If the array contains an odd number of elements, the median is the middle element in the sorted array.
  2. If the array has an even number of elements, the median is the average of the two middle elements in the sorted array.

Let us take a simple example.

Consider the array {1, 2, 3, 4, 5}. 

Median of a sorted array

Since the array is of odd length, the median of the array is simply the middle element that is, 3.

Naive Approach

Before delving into the optimized approach, let’s briefly discuss the naive approach and its limitations. The naive approach involves merging the two sorted arrays into a single array and then finding the median of the merged array. 

Since the arrays are already sorted, we will simply maintain two pointers at the initial of both arrays move them accordingly, and add them into the new merged array.

  • We will maintain two pointers and keep them at the 0th index of both arrays.
0th index of both array
  • We will then compare the integers add the smaller integer into the merged array and move the pointer one index ahead
add the smaller integer into the merged array
  • Similarly, we will traverse both arrays and make the merged array.
  • Once the merged array is formed we will find the median of the array as discussed above.
  • Therefore, the median = 4+5/2 = 4
Merged array and its median

This approach has a time complexity of O(n+k) as we need to traverse both arrays to merge them and then find the median. Since we are taking an extra array of size n+k, the space complexity is O(n+k).

Optimized Approach

The optimized approach is based on the concept of divide and conquer, similar to binary search. The main idea is to divide the arrays into two parts and compare the medians of both arrays. Based on the comparison, we can discard certain elements and narrow down the search space until we find the median.

Let’s get into the depth with an example:

Array1 = {1, 3, 7, 8}

Array2 = {2, 4, 6}

Example of optimized approach

Given the two arrays, let us try to find our answer.

  1. The combined length of the arrays is 3+4 = 7.
  2. Therefore, we know that our median will be at the 7/2 = 4th position of the merged array.
  3. Let us try not to use extra space and apply the divide and conquer algorithm.
  4. We will try to form symmetric spaces keeping 3 elements in left space and 4 elements in the right space.
  5. Since the length of the second is 3, we can determine that there must be at least 1 element of the array 1 in the left space.
Case 1: 4th position
  1. Thus, considering array 1, it can have between 0 to 6 elements.
  2. We will thus apply a Binary search to figure out how many elements to select from array 1.
  3. Low = 0, High = 3. So, mid = 2.
  4. Now we will update the mid as follows:
  • If L1>R1, high = mid – 1.
  • If L2>R2, low = mid+1.
Case 2: Mid = 2
  1. Here, we can see that both L1 and L2 are lesser than R1 and R2. Therefore, the left and right segments are sorted.
  2. Now, we can find the median at min(R1, R2)
Median of array: 4

C++ Code

Here is the C++ program to find median of two sorted arrays using an optimized approach:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    // Ensure nums1 is the smaller array
    if (nums1.size() > nums2.size()) {
        swap(nums1, nums2);
    }

    int m = nums1.size();
    int n = nums2.size();
    int left = 0, right = m, partitionX, partitionY;
    
    while (left <= right) {
        // Choose a partition index in the smaller array nums1
        partitionX = (left + right) / 2;
        // Calculate the corresponding partition index in nums2
        partitionY = (m + n + 1) / 2 - partitionX;
        
        // Calculate maximum and minimum values in the left and right partitions
        int maxX = (partitionX == 0) ? INT_MIN : nums1[partitionX - 1];
        int maxY = (partitionY == 0) ? INT_MIN : nums2[partitionY - 1];
        int minX = (partitionX == m) ? INT_MAX : nums1[partitionX];
        int minY = (partitionY == n) ? INT_MAX : nums2[partitionY];
        
        // Check if the partitions are correct
        if (maxX <= minY && maxY <= minX) {
            // Calculate the median
            if ((m + n) % 2 == 0) {
                return (max(maxX, maxY) + min(minX, minY)) / 2.0;
            } 
          	else {
                return max(maxX, maxY);
            }
        }
      	else if (maxX > minY) {
            // Adjust the partition indexes
            right = partitionX - 1;
        } 
      	else {
            left = partitionX + 1;
        }
    }
    
    return 0.0;  // Return 0.0 if no median found
}

int main() {
    vector<int> nums1 = {1, 3, 7, 8};
    vector<int> nums2 = {2, 4, 6};
    double median = findMedianSortedArrays(nums1, nums2);
    cout << "Median: " << median << endl;
    return 0;
}

Python Code

Below is the Python program using an optimized approach:

def findMedianSortedArrays(nums1, nums2):
    # Ensure nums1 is the smaller array
    if len(nums1) > len(nums2):
        nums1, nums2 = nums2, nums1

    m, n = len(nums1), len(nums2)
    left, right, partitionX, partitionY = 0, m, 0, 0

    while left <= right:
        # Choose a partition index in the smaller array nums1
        partitionX = (left + right) // 2
        # Calculate the corresponding partition index in nums2
        partitionY = (m + n + 1) // 2 - partitionX

        # Calculate maximum and minimum values in the left and right partitions
        maxX = float('-inf') if partitionX == 0 else nums1[partitionX - 1]
        maxY = float('-inf') if partitionY == 0 else nums2[partitionY - 1]
        minX = float('inf') if partitionX == m else nums1[partitionX]
        minY = float('inf') if partitionY == n else nums2[partitionY]

        # Check if the partitions are correct
        if maxX <= minY and maxY <= minX:
            # Calculate the median
            if (m + n) % 2 == 0:
                return (max(maxX, maxY) + min(minX, minY)) / 2.0
            else:
                return float(max(maxX, maxY))
        elif maxX > minY:
            # Adjust the partition indexes
            right = partitionX - 1
        else:
            left = partitionX + 1

    return 0.0  # Return 0.0 if no median found

nums1 = [1, 3, 7, 8]
nums2 = [2, 4, 6]
median = findMedianSortedArrays(nums1, nums2)
print("Median:", median)

Java Code

Check the Java program below to find the median of two sorted arrays:

public class MedianOfTwoSortedArrays {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        // Ensure nums1 is the smaller array
        if (nums1.length > nums2.length) {
            int[] temp = nums1;
            nums1 = nums2;
            nums2 = temp;
        }

        int m = nums1.length;
        int n = nums2.length;
        int left = 0;
        int right = m;
        int partitionX, partitionY;

        while (left <= right) {
            // Choose a partition index in the smaller array nums1
            partitionX = (left + right) / 2;
            // Calculate the corresponding partition index in nums2
            partitionY = (m + n + 1) / 2 - partitionX;

            // Calculate maximum and minimum values in the left and right partitions
            int maxX = (partitionX == 0) ? Integer.MIN_VALUE : nums1[partitionX - 1];
            int maxY = (partitionY == 0) ? Integer.MIN_VALUE : nums2[partitionY - 1];
            int minX = (partitionX == m) ? Integer.MAX_VALUE : nums1[partitionX];
            int minY = (partitionY == n) ? Integer.MAX_VALUE : nums2[partitionY];

            // Check if the partitions are correct
            if (maxX <= minY && maxY <= minX) {
                // Calculate the median
                if ((m + n) % 2 == 0) {
                    return (double) (Math.max(maxX, maxY) + Math.min(minX, minY)) / 2.0;
                } 
              	else {
                    return (double) Math.max(maxX, maxY);
                }
            } 
          	else if (maxX > minY) {
                // Adjust the partition indexes
                right = partitionX - 1;
            } 
          	else {
                left = partitionX + 1;
            }
        }

        return 0.0;  // Return 0.0 if no median found
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 3, 7, 8};
        int[] nums2 = {2, 4, 6};
        double median = findMedianSortedArrays(nums1, nums2);
        System.out.println("Median: " + median);
    }
}

Output:

Median: 4.0

The time complexity of this logarithmic approach is O(log(min(n, k))), where n and k are the sizes of the two arrays. Since we are dividing the search space in half at each step, the algorithm converges to the median in logarithmic time.

The space complexity of this algorithm is O(1), as it only requires a constant amount of extra space to store the variables.

Analysis of Both Approaches

Let’s see a quick review of all the approaches.

ApproachTime ComplexitySpace ComplexityDetails
Naive ApproachO(n+k)O(n+k)It involves merging the two sorted arrays into a single array and finding the median of the merged array. 
Divide and Conquer ApproachO(log(n+k))O(1)This approach involves dividing the arrays into two parts and comparing the medians of both arrays based on divide and conquer strategy.

Conclusion

In this article, we learned how to find the median of a sorted array. By leveraging the divide and conquer strategy, we were able to efficiently narrow down the search space and find it in O(log(n+k)) time. Solving this problem efficiently is not only a demonstration of algorithmic processes but also has practical applications in various domains, including data analysis, numerical computing, and more.

ShareTweetShareSendSend
Piyush Kaushal

Piyush Kaushal

I am Piyush Kaushal, currently pursuing a degree in software engineering at a prestigious government university. I am dedicated to staying informed about the latest technological developments and continuously expanding my knowledge base. I take great pleasure in sharing my expertise in data science and coding with fellow aspiring minds.

RelatedPosts

validate ip address

Validate IP Address Problem ( C++, JAVA, Python)

April 3, 2025
Shortest Palindrome Problem

Shortest Palindrome Problem ( C++, JAVA, Python)

April 4, 2025
Zigzag Conversion Problem

Zigzag Conversion Problem (C++, Java, Python)

April 4, 2025
Next Right Pointers In Each Node

Populating Next Right Pointers In Each Node (with code)

March 27, 2024
Coin Change II

Coin Change II Problem (C++, Java, Python)

April 7, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.