Skip to content

Create median of two sorted arrays #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions LeetCodeQuestions/median of two sorted arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
/* brute force , tc - O(m + n) , sc - O(n)
int m = nums1.length;
int n= nums2.length;
int res[] = new int[m + n];

int k = 0;
for(int i = 0; i < m; i++ ){
res[k++] = nums1[i];
}

for( int i = 0;i < n; i++){
res[k++] = nums2[i];
}

Arrays.sort(res);



int mid = res.length/2;

if(res.length % 2 == 0){
return (double) (res[mid -1 ] + res[mid])/2;
}
return res[mid];





linear search (better approach) , tc - O(n) , sc - O(1)
int n1 = nums1.length;
int n2 = nums2.length;
int count = 0;
int i = 0,j =0;
int mid = (n1 + n2) /2;
int ele1 = -1;
int ele2 = -1;

while(i < n1 && j < n2 ){
if(nums1[i] < nums2[j]){
if( count == mid -1)ele1 = nums1[i];
else if ( count == mid)ele2 = nums1[i];
i++;
count++;
}
else{
if( count == mid -1)ele1 = nums2[j];
else if( count == mid)ele2 = nums2[j];
j++;
count++;
}



}
while( i < n1){
if( count == mid-1)ele1= nums1[i];
else if( count == mid)ele2 = nums1[i];
i++;
count++;
}

while( j < n2){
if( count == mid-1)ele1= nums2[j];
else if( count == mid)ele2 = nums2[j];
j++;
count++;
}

if( (n1 + n2) % 2 == 0 ){
return 1D * (ele1 + ele2)/2;
}
return 1D * ele2;
*/

// optimal approach binary search ,tc - O(log n) , sc - O(1)

int n1 = nums1.length, n2 = nums2.length;

// Ensure nums1 is the smaller array for simplicity
if (n1 > n2)
return findMedianSortedArrays(nums2, nums1);

int n = n1 + n2;
int left = (n1 + n2 + 1) / 2; // Calculate the left partition size
int low = 0, high = n1;

while (low <= high) {
int mid1 = (low + high) >> 1; // Calculate mid index for nums1
int mid2 = left - mid1; // Calculate mid index for nums2

int l1 = Integer.MIN_VALUE, l2 = Integer.MIN_VALUE, r1 = Integer.MAX_VALUE, r2 = Integer.MAX_VALUE;

// Determine values of l1, l2, r1, and r2
if (mid1 < n1)
r1 = nums1[mid1];
if (mid2 < n2)
r2 = nums2[mid2];
if (mid1 - 1 >= 0)
l1 = nums1[mid1 - 1];
if (mid2 - 1 >= 0)
l2 = nums2[mid2 - 1];

if (l1 <= r2 && l2 <= r1) {
// The partition is correct, we found the median
if (n % 2 == 1)
return Math.max(l1, l2);
else
return ((double)(Math.max(l1, l2) + Math.min(r1, r2))) / 2.0;
}
else if (l1 > r2) {
// Move towards the left side of nums1
high = mid1 - 1;
}
else {
// Move towards the right side of nums1
low = mid1 + 1;
}
}

return 0; // If the code reaches here, the input arrays were not sorted.


}


}