
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find median of two sorted lists in C++
Suppose we have two sorted lists. We have to find the median of these two lists. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.
To solve this, we will follow these steps:
- Define a function solve(), this will take an array nums1, an array nums2,
- if size of nums1 > size of nums2, then:
- return solve(nums2, nums1)
- x := size of nums1, y := size of nums2
- low := 0, high := x
- totalLength := x + y
- while low <= high, do:
- partitionX := low + (high - low)
- partitionY := (totalLength + 1) / 2 - partitionX
- maxLeftX := (if partitionX is same as 0, then -inf, otherwise nums1[partitionX - 1])
- minRightX := (if partitionX is same as x, then inf, otherwise nums1[partitionX])
- maxLeftY := (if partitionY is same as 0, then -inf, otherwise nums2[partitionY - 1])
- minRightY := (if partitionY is same as y, then inf, otherwise nums2[partitionY])
- if maxLeftX <= minRightY and maxLeftY <= minRightX, then:
- if totalLength mod 2 is same as 0, then:
- return ((maximum of maxLeftX and maxLeftY) + (minimum of minRightX and minRightY))/ 2
- Otherwise
- return maximum of maxLeftX and maxLeftY
- if totalLength mod 2 is same as 0, then:
- otherwise when maxLeftX > minRightY, then:
- high := partitionX - 1
- Otherwise
- return 0
Let us see the following implementation to get better understanding:
Example
#include using namespace std; class Solution { public: double solve(vector& nums1, vector& nums2) { if(nums1.size()>nums2.size()) return solve(nums2,nums1); int x = nums1.size(); int y = nums2.size(); int low = 0; int high = x; int totalLength = x+y; while(low<=high){ int partitionX = low + (high - low)/2; int partitionY = (totalLength + 1)/2 - partitionX; int maxLeftX = (partitionX ==0?INT_MIN:nums1[partitionX-1] ); int minRightX = (partitionX == x?INT_MAX : nums1[partitionX]); int maxLeftY = (partitionY ==0?INT_MIN:nums2[partitionY-1] ); int minRightY = (partitionY == y?INT_MAX : nums2[partitionY]); if(maxLeftX<=minRightY && maxLeftY <= minRightX){ if(totalLength% 2 == 0){ return ((double)max(maxLeftX,maxLeftY) + (double)min(minRightX,minRightY))/2; } else{ return max(maxLeftX, maxLeftY); } } else if(maxLeftX>minRightY) high = partitionX-1; else low = partitionX+1; } return 0; } }; main(){ Solution ob; vector v1 = {1,5,8}, v2 = {2,3,6,9}; cout << (ob.solve(v1, v2)); }
Input
[1,5,8], [2,3,6,9]
Output
5
- Related Articles
- Median of two sorted array
- C++ Program to find the median of two sorted arrays using binary search approach
- Median of Two Sorted Arrays in C++
- JavaScript Program to Find median in row wise sorted matrix
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- Python program to create a sorted merged list of two unsorted lists
- Merge Two Sorted Lists in Python
- Combining two sorted lists in Python
- C/C++ Program for Median of two sorted arrays of same size?
- C# program to find Intersection of two lists
- Python program to find Intersection of two lists?
- Program to merge K-sorted lists in Python
- Python program to find Cartesian product of two lists
- Merge two sorted linked lists using C++.
- C# program to find additional values in two lists

Advertisements