Java Binary search
\nclass Solution {\n    public int maxDistance(int[] nums1, int[] nums2) {\n        int max = 0;\n        for (int i = 0; i < nums1.length; i++) {\n            
```\nclass Solution {\n    public int maxDistance(int[] nums1, int[] nums2) {\n        int max = 0;\n        for (int i = 0; i < nums1.length; i++) {\n            int r = nums2.length - 1;\n            int l = i;\n            int m = i;\n            while (l <= r) {\n                m = l + (r - l) / 2;\n                if (nums1[i] > nums2[m]) {\n                    r = m - 1;\n                } else if (nums1[i] == nums2[m]) {\n                    l = m + 1;\n                } else {\n                    l = m + 1;\n                }\n            }\n            if (r < 0) {\n                continue;\n            }\n            max = Math.max(max, r - i);\n        }\n        return max;\n    }\n}\n```
['Binary Tree', 'Java']