O ( N ) || O ( N LOG N) || Both Approaches
O ( N LOG N ) \n BINARY SEARCH in C++\n\n\nclass Solution {\npublic:\nint maxDistance(vector<int>& n1, vector<int>& n2) {\n\n    int ans=0,k=min(n1.size(),n2.si
**O ( N LOG N ) \n BINARY SEARCH in C++**\n\n```\nclass Solution {\npublic:\nint maxDistance(vector<int>& n1, vector<int>& n2) {\n\n    int ans=0,k=min(n1.size(),n2.size());\n    for(int i=0; i<k;i++){\n            int l=i,r=n2.size()-1,m;\n        while(l<=r){\n            m=l+(r-l)/2;\n            int t= n1[i];\n            if(n2[m]>=t){\n                l=m+1;\n                if(m-i>ans)ans=m-i;\n            } \n            else r=m-1;\n        }  \n    }\n    return ans;\n    }\n};\n```\n\n\n**O ( N ) in PYTHON**\n```\nclass Solution:\n    def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:\n        i=0\n        j=0\n        k=0\n        while j<len(nums2):\n            while i<len(nums1) and nums1[i]>nums2[j]:\n                i+=1\n            if (i<len(nums1)):\n                k=max(k,j-i)\n            j+=1\n            if i==len(nums1):\n                break\n        return k\n```\n\n**O ( N ) in C++**\n```\nclass Solution {\npublic:\n    int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n        int i=0;\n        int j=0;\n        int k=0;\n        while (j<nums2.size()){\n            while (i<nums1.size() && nums1[i]>nums2[j]){\n                i++;\n            }\n            k = max(k,j-i);\n            j++;\n            if (i>=nums1.size()){\n                break;\n            }\n        }\n        return k;\n    }\n};\n```\n![image](https://assets.leetcode.com/users/images/789f0317-0f2d-42da-9bb8-ac6a99c86144_1653716978.201911.jpeg)\n
['Two Pointers', 'C', 'Binary Tree', 'Python', 'C++']