Java solution | Brute force | Binary Search Solution | 2 approaches
Brute force approach: Gives TLE\n\n\npublic int maxDistance(int[] nums1, int[] nums2) {\n        int maxDistance = 0;\n        int len1 = nums1.length;\n       
Brute force approach: Gives TLE\n\n```\npublic int maxDistance(int[] nums1, int[] nums2) {\n        int maxDistance = 0;\n        int len1 = nums1.length;\n        int len2 = nums2.length;\n        for(int i=0;i<len1;i++){\n            for(int j=i;j<len2;j++){\n                if(nums1[i]<=nums2[j]){\n                    maxDistance = Math.max(maxDistance, j-i);\n                }\n            }\n        }\n        return maxDistance;\n    }\n```\n\nBinary search solution:\n\n```\npublic int maxDistance(int[] nums1, int[] nums2) {\n        int maxDistance = 0;\n        int len1 = nums1.length;\n        int len2 = nums2.length;\n        for(int i=0;i<len1;i++){\n            int low = i;\n            int high = len2-1;\n            while(low<=high){\n                int mid = low+(high-low)/2;\n                if(nums1[i] <= nums2[mid]){\n                    maxDistance = Math.max(maxDistance, mid-i);\n                    low = mid+1;\n                }\n                else{\n                    high = mid-1;\n                }\n            }\n        }\n        return maxDistance;\n    }\n```\n\n
['Binary Tree', 'Java']