Binary Search , Easy C++ ✅✅
Approach\n Describe your approach to solving the problem. \nHere both the Array are sorted, so we can solve this problem in O(N*Log(N)) time using binary search
# Approach\n<!-- Describe your approach to solving the problem. -->\nHere both the Array are **sorted**, so we can solve this problem in **O(N*Log(N))** time using **binary search**.\n\n**For example:- Array-1 = [55,30,5,4,2], Array-2 = [100,20,10,10,5]**\nFirst traverse the array from 0 to n and find the index value j **(such as j>=i and Array-2[j]>=Array-1[i])** using **binary search**.\n\n\n\n\n# Complexity\n- Time complexity:O(N*Log(N))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxDistance(vector<int>& arr1, vector<int>& arr2) {\n        int n = arr1.size(),m = arr2.size(),ans = 0;\n        for(int i = 0;i<n;i++)\n        {\n            int ind = -1,left = i,right = m-1;\n            while(left<=right)\n            {\n              int mid = (left+right)/2;\n              if(arr2[mid]>=arr1[i])\n              {\n                ind = mid;\n                left = mid+1;\n              }\n              else\n              right = mid-1;\n            }\n            if(ind!=-1) ans = max(ans,ind-i);\n        }\n        return ans;\n    }\n};\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/bcc47018-8ac2-48db-8231-fe92968317e6_1689065040.8459961.jpeg)\n
['C++']