From 3f51e62cb6aaa934671dc2352653fb2c80c7609e Mon Sep 17 00:00:00 2001 From: Arunima Dutta Date: Sun, 23 Jun 2024 23:35:32 +0530 Subject: [PATCH 1/2] Create median-of-2-sorted-arrays-of-different-sizes --- ...dian-of-2-sorted-arrays-of-different-sizes | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes b/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes new file mode 100644 index 000000000..accabe3f9 --- /dev/null +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes @@ -0,0 +1,78 @@ +//{ Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; +double MedianOfArrays(vector& array1, vector& array2); + + +// } Driver Code Ends +//User function Template for C++ + +class Solution { +public: + double MedianOfArrays(std::vector& array1, std::vector& array2) { + if (array1.size() > array2.size()) { + return MedianOfArrays(array2, array1); + } + + int m = array1.size(); + int n = array2.size(); + int low = 0, high = m; + + while (low <= high) { + int partition1 = (low + high) / 2; + int partition2 = (m + n + 1) / 2 - partition1; + + int maxLeft1 = (partition1 == 0) ? INT_MIN : array1[partition1 - 1]; + int minRight1 = (partition1 == m) ? INT_MAX : array1[partition1]; + + int maxLeft2 = (partition2 == 0) ? INT_MIN : array2[partition2 - 1]; + int minRight2 = (partition2 == n) ? INT_MAX : array2[partition2]; + + if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { + if ((m + n) % 2 == 0) { + return (std::max(maxLeft1, maxLeft2) + std::min(minRight1, minRight2)) / 2.0; + } else { + return std::max(maxLeft1, maxLeft2); + } + } else if (maxLeft1 > minRight2) { + high = partition1 - 1; + } else { + low = partition1 + 1; + } + } + + throw std::invalid_argument("Input arrays are not sorted"); + } +}; + + +//{ Driver Code Starts. + +int main() +{ + int t; + cin>>t; + while(t--) + { + int m,n; + cin>>m; + vector array1(m); + for (int i = 0; i < m; ++i){ + cin>>array1[i]; + } + cin>>n; + vector array2(n); + for (int i = 0; i < n; ++i){ + cin>>array2[i]; + } + Solution ob; + cout< Date: Mon, 24 Jun 2024 09:51:58 +0530 Subject: [PATCH 2/2] Update median-of-2-sorted-arrays-of-different-sizes The solution is updated in the given format --- ...dian-of-2-sorted-arrays-of-different-sizes | 275 +++++++++++++----- 1 file changed, 197 insertions(+), 78 deletions(-) diff --git a/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes b/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes index accabe3f9..93f84f038 100644 --- a/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes +++ b/dsa-solutions/gfg-solutions/Hard/0101-0200/median-of-2-sorted-arrays-of-different-sizes @@ -1,78 +1,197 @@ -//{ Driver Code Starts -//Initial Template for C++ - -#include -using namespace std; -double MedianOfArrays(vector& array1, vector& array2); - - -// } Driver Code Ends -//User function Template for C++ - -class Solution { -public: - double MedianOfArrays(std::vector& array1, std::vector& array2) { - if (array1.size() > array2.size()) { - return MedianOfArrays(array2, array1); - } - - int m = array1.size(); - int n = array2.size(); - int low = 0, high = m; - - while (low <= high) { - int partition1 = (low + high) / 2; - int partition2 = (m + n + 1) / 2 - partition1; - - int maxLeft1 = (partition1 == 0) ? INT_MIN : array1[partition1 - 1]; - int minRight1 = (partition1 == m) ? INT_MAX : array1[partition1]; - - int maxLeft2 = (partition2 == 0) ? INT_MIN : array2[partition2 - 1]; - int minRight2 = (partition2 == n) ? INT_MAX : array2[partition2]; - - if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { - if ((m + n) % 2 == 0) { - return (std::max(maxLeft1, maxLeft2) + std::min(minRight1, minRight2)) / 2.0; - } else { - return std::max(maxLeft1, maxLeft2); - } - } else if (maxLeft1 > minRight2) { - high = partition1 - 1; - } else { - low = partition1 + 1; - } - } - - throw std::invalid_argument("Input arrays are not sorted"); - } -}; - - -//{ Driver Code Starts. - -int main() -{ - int t; - cin>>t; - while(t--) - { - int m,n; - cin>>m; - vector array1(m); - for (int i = 0; i < m; ++i){ - cin>>array1[i]; - } - cin>>n; - vector array2(n); - for (int i = 0; i < n; ++i){ - cin>>array2[i]; - } - Solution ob; - cout< + + + + ```py + class Solution: + def MedianOfArrays(self, array1, array2): + if len(array1) > len(array2): + array1, array2 = array2, array1 + + m, n = len(array1), len(array2) + imin, imax, half_len = 0, m, (m + n + 1) // 2 + + while imin <= imax: + i = (imin + imax) // 2 + j = half_len - i + + if i < m and array1[i] < array2[j-1]: + imin = i + 1 + elif i > 0 and array1[i-1] > array2[j]: + imax = i - 1 + else: + if i == 0: max_of_left = array2[j-1] + elif j == 0: max_of_left = array1[i-1] + else: max_of_left = max(array1[i-1], array2[j-1]) + + if (m + n) % 2 == 1: + return max_of_left + + if i == m: min_of_right = array2[j] + elif j == n: min_of_right = array1[i] + else: min_of_right = min(array1[i], array2[j]) + + return (max_of_left + min_of_right) / 2.0 + ``` + + + + + + ```cpp + #include + #include + #include + #include + #include + + class Solution { + public: + double MedianOfArrays(std::vector& array1, std::vector& array2) { + if (array1.size() > array2.size()) { + return MedianOfArrays(array2, array1); + } + + int m = array1.size(); + int n = array2.size(); + int low = 0, high = m; + + while (low <= high) { + int partition1 = (low + high) / 2; + int partition2 = (m + n + 1) / 2 - partition1; + + int maxLeft1 = (partition1 == 0) ? INT_MIN : array1[partition1 - 1]; + int minRight1 = (partition1 == m) ? INT_MAX : array1[partition1]; + + int maxLeft2 = (partition2 == 0) ? INT_MIN : array2[partition2 - 1]; + int minRight2 = (partition2 == n) ? INT_MAX : array2[partition2]; + + if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) { + if ((m + n) % 2 == 0) { + return (std::max(maxLeft1, maxLeft2) + std::min(minRight1, minRight2)) / 2.0; + } else { + return std::max(maxLeft1, maxLeft2); + } + } else if (maxLeft1 > minRight2) { + high = partition1 - 1; + } else { + low = partition1 + 1; + } + } + + throw std::invalid_argument("Input arrays are not sorted"); + } + }; + + // Example usage: + int main() { + std::vector array1 = {1, 3}; + std::vector array2 = {2}; + + Solution sol; + double median = sol.MedianOfArrays(array1, array2); + std::cout << "Median: " << median << std::endl; // Expected output: 2.0 + + return 0; + } + ``` + + + + + +## Example Walkthrough + +For the arrays `array1 = [1, 3]` and `array2 = [2]`: + +1. Combined array would be `[1, 2, 3]`. +2. The median is `2.0`. + +For the arrays `array1 = [1, 2]` and `array2 = [3, 4]`: + +1. Combined array would be `[1, 2, 3, 4]`. +2. The median is `(2 + 3) / 2 = 2.5`. + +## Solution Logic: + +1. Ensure `array1` is the smaller array to minimize the number of binary search steps. +2. Perform binary search on the smaller array. +3. Calculate partitions for both arrays such that left and right parts of the partitions can be merged to form the sorted order. +4. Handle edge cases where partitions are at the boundaries of the arrays. +5. If the total number of elements is even, the median is the average of the maximum of the left parts and the minimum of the right parts. +6. If the total number of elements is odd, the median is the maximum of the left parts. + +## Time Complexity + +* The primary operation is binary search, which has a time complexity of $O(log(min(m, n)))$, where m and n are the sizes of the arrays. + +## Space Complexity + +Auxiliary Space: The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input arrays. + +## References + +- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/median-of-2-sorted-arrays-of-different-sizes/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=bottom_sticky_on_article) +- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/) +``` + +In this format, the tutorial includes a description of the problem, examples, expected time and space complexity, constraints, detailed problem explanation, and complete code implementations in both Python and C++. It ends with a walkthrough of examples to illustrate the logic of the solution.