java | Brute Force | Two Pointers | Binary Search
\nclass Solution {\n    public int maxDistance(int[] nums1, int[] nums2) {\n        \n        //BinarySearch : TC=O(mlogn), SC=O(1)\n        int d = 0;\n       
```\nclass Solution {\n    public int maxDistance(int[] nums1, int[] nums2) {\n        \n        //BinarySearch : TC=O(mlogn), SC=O(1)\n        int d = 0;\n        for(int i=0;i<nums1.length;i++){\n            int start = i;\n            int end = nums2.length-1;\n            while(start <= end){\n                int mid = start +  (end-start)/2;\n                if(nums1[i] <= nums2[mid]){\n                    d = Math.max(d,mid-i);\n                    start = mid+1;\n                } else {\n                    end = mid-1;\n                }\n            }\n        }\n        return d;\n        \n        /*\n        //Two Pointers Approach : TC=O(min(m,n)), SC=O(1)\n        int i=0,j=0,d=0;\n        while(i<nums1.length && j<nums2.length){\n            if(i<=j && nums1[i]<=nums2[j]){\n                d=Math.max(d,j-i);\n                j++;\n                \n            } else if(nums1[i] > nums2[j]){\n                i++;\n            }else{\n                j++;\n            }\n        }\n        return d;\n        */\n        \n        /*\n        //BruteForce Approach : TC=O(m*n) , SC=O(1) -> TLE\n        int d = 0;\n        for(int i=0;i<nums1.length;i++){\n            for(int j=0;j<nums2.length;j++){\n                if(i<=j && nums1[i]<=nums2[j]){\n                    d = Math.max(d,j-i);\n                }\n            }\n        }\n        return d;\n        */\n    }\n}\n```
['Two Pointers', 'Binary Tree', 'Java']