Skip to content

Commit f1156f3

Browse files
Merge pull request #550 from Shashank0271/lc1
added java Solution for Leetcode #4
2 parents 42eef4c + 507b588 commit f1156f3

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

0 commit comments

Comments
 (0)