# Easy O(n) java solution

• public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int totalLength = nums1.length + nums2.length;
int i=0, j=0;
int count = 0;
int last = 0;
double mid = last;
// iterate through the nums array put the smaller element to the left side of midian
while((i+j)<totalLength/2)
{
if (i<nums1.length && j<nums2.length)
if (nums1[i]<nums2[j])
{
last = nums1[i];
i++;
}
else
{
last = nums2[j];
j++;
}
else if (i<nums1.length && j==nums2.length)
{
last = nums1[i];
i++;
}
else if (i==nums1.length && j<nums2.length)
{
last = nums2[j];
j++;
}
else break;
}
int last2;
//check if i or j is out of bound, mark the smaller element of i/j as the other mid candidate
if (i==nums1.length) last2 = nums2[j];
else if (j==nums2.length) last2 = nums1[i];
else last2=Math.min(nums1[i], nums2[j]);

``````    // check if the total length is even, then mid is the middle of two candidate
if (2*(i+j) == totalLength)
{
mid = (last+last2)/2.0;
}
else
{
// otherwise mid is the i/j candidate
mid = last2;
}
return mid;
}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.