O(m+n) solution to median problem