File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Problem link : https://leetcode.com/problems/median-of-two-sorted-arrays/description
2
+
3
+ class Solution {
4
+ public double findMedianSortedArrays (int [] nums1 , int [] nums2 ) {
5
+ int arr []=new int [nums1 .length +nums2 .length ];
6
+
7
+ int j =0 ;
8
+ int i =0 ;
9
+ int r =0 ;
10
+ while (i <nums1 .length && j <nums2 .length )
11
+ {
12
+ if (nums1 [i ]<nums2 [j ])
13
+ {
14
+ arr [r ]=nums1 [i ];
15
+ i ++;
16
+ r ++;
17
+ }
18
+ else if (nums1 [i ]>nums2 [j ])
19
+ {
20
+ arr [r ]=nums2 [j ];
21
+ j ++;
22
+ r ++;
23
+ }
24
+ else
25
+ {
26
+ arr [r ]=nums1 [i ];
27
+ r ++;
28
+ arr [r ]=nums2 [j ];
29
+ r ++;
30
+ i ++;
31
+ j ++;
32
+ }
33
+
34
+ }
35
+ while (i <nums1 .length )
36
+ {
37
+ arr [r ]=nums1 [i ];
38
+ r ++;
39
+ i ++;
40
+ }
41
+ while (j <nums2 .length )
42
+ {
43
+ arr [r ]=nums2 [j ];
44
+ r ++;
45
+ j ++;
46
+ }
47
+ if (arr .length %2 ==0 )
48
+ {
49
+
50
+ double a1 = Double .valueOf ( arr [arr .length /2 ]);
51
+ double a2 =Double .valueOf (arr [arr .length /2 -1 ]);
52
+ double d = (a1 +a2 )/2 ;
53
+ return d ;
54
+ }
55
+ else
56
+ {
57
+ return Double .valueOf (arr [arr .length /2 ]);
58
+ }
59
+ }
60
+ }
You can’t perform that action at this time.
0 commit comments