Skip to content

Commit e2f15f8

Browse files
Update Median_of_two_sorted_array.java
1 parent 1922639 commit e2f15f8

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Arrays/Median_of_two_sorted_array.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Solution {
22
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
3+
//Creating a ArrayList containing all the elements of both array
34
List<Integer> merged = new ArrayList<Integer>();
45
for (int i= 0 ; i < nums1.length; i++){
56
merged.add(nums1[i]);
@@ -8,10 +9,14 @@ public double findMedianSortedArrays(int[] nums1, int[] nums2) {
89
merged.add(nums2[i]);
910
}
1011
Collections.sort(merged);
12+
//Sorting the finally Merged Linked List
1113
int idx = merged.size()/2;
14+
//Finding the middle index to get the Median
1215
if (merged.size()%2 != 0){
16+
//If Size of arraylist is odd then the middle element will be the median of the ArrayList.
1317
return merged.get(idx);
1418
}
19+
1520
return (merged.get(idx-1)+merged.get(idx))/2.0;
1621

1722
}

0 commit comments

Comments
 (0)