Skip to content

Commit 6c42f65

Browse files
Added Code For Median of Two Sorted Arrays (#61)
* Added code for median of two sorted arrays * Error fixed * Error fixed
1 parent bd33b29 commit 6c42f65

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Diff for: Java/median-of-two-sorted-arrays.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
3+
4+
//Easy for Binary search on smaller length array
5+
//Checking the length and calling the main Function with smaller len array as forst array
6+
if(nums1.length > nums2.length){
7+
return findMedianSortedArrays(nums2,nums1);
8+
}
9+
10+
int len1 = nums1.length;
11+
int len2 = nums2.length;
12+
//System.out.println(len1 + " " + len2);
13+
14+
//Doing Binary search on smaller array
15+
int start = 0;
16+
int end = len1;
17+
while(start<=end){
18+
int partion1 = (start+end)/2;
19+
int partion2 = ((len1+len2+1)/2 - partion1);
20+
21+
//finding the max and min lenemt is num1 array
22+
int max1 = (partion1 == 0 ) ? Integer.MIN_VALUE : nums1[partion1-1];
23+
int min1 = (partion1 == len1) ? Integer.MAX_VALUE : nums1[partion1];
24+
25+
//finding the max and min elemnt of nums2 array
26+
int max2 = (partion2 == 0 ) ? Integer.MIN_VALUE : nums2[partion2-1];
27+
//System.out.println(Integer.MIN_VALUE + " " + Integer.MAX_VALUE);
28+
int min2 = (partion2 == len2) ? Integer.MAX_VALUE : nums2[partion2];
29+
30+
if(max1 <= min2 && max2 <= min1){
31+
if((len1 + len2 ) %2 == 0){
32+
return (double)(Math.max(max1, max2) + Math.min(min1, min2))/2;
33+
}else{
34+
return (double)Math.max(max1,max2);
35+
}
36+
}else if( max1 > min2){
37+
end = partion1 - 1;
38+
}else{
39+
start = partion1 + 1;
40+
}
41+
}
42+
return 0;
43+
}
44+
}

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
216216
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
217217
| --- | --------------------------------------------------------------------------------------------- | --------------------------------------------------- | -------- | ------ | ---------- | --- | ---------------- |
218218
| 005 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Python](./Python/5_LongestPalindromicSubstring.py) | _O(N^2)_ | _O(N)_ | Medium | | Expand the Wings |
219-
219+
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [Java](./Java/median-of-two-sorted-arrays.java) | _O(log(min(m,n)))_ | _O(1)_ | Hard | | |
220220
<br/>
221221
<div align="right">
222222
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -371,7 +371,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
371371
| [Fenil Dobariya](https://github.com/ifenil) <br> <img src="https://rb.gy/iascx3" width="100" height="100"> | India | Java | [Github](https://github.com/ifenil) |
372372
| [Prashansa Tanwar](https://github.com/prashansatanwar) <br> <img src="https://github.com/prashansatanwar.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/prashansaaa/) |
373373
| [Ishu Raj](https://github.com/ir2010) <br> <img src="https://github.com/ir2010.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishuraj2010/) |
374-
374+
| [Rakesh Bhadhavath](https://github.com/Revenge-Rakesh) <br> <img src="https://avatars2.githubusercontent.com/u/36032275?v=4" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/goal_cracker/) |
375375

376376
<br/>
377377
<div align="right">

0 commit comments

Comments
 (0)