Binary Search||Lower_bound||C++
EXPLANATION:The problem demands that for any element nums1[i], we have to find the farthest element nums2[j] with j>=i such that the nums2[j] element is not les
**EXPLANATION:**The problem demands that for any element nums1[i], we have to find the farthest element nums2[j] with j>=i such that the nums2[j] element is not less than nums1[i].\n\nA brute force way of doing the same would be to iterate over every j in the range i <=j && j <n" , but then we aren\'t using the fact that the two arrays are sorted in non-increasing/decreasing order.\n\nSo, what we do is, reverse the second array to make it increasing and then for every nums1[i], we find the first element which is either equal to or greater than it i.e. Lower_bound of the element (Note that for the original array this would be the same element which would be farther away satisfying the condition).\n\n\n```\nclass Solution {\npublic:\n    int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n        \n        reverse(nums2.begin(),nums2.end());\n        int ans = 0;\n        for(int i=0;i<nums1.size();++i){\n            auto it = lower_bound(nums2.begin(),nums2.end(),nums1[i]) - nums2.begin();  //Finds first element greater than or equal to nums1[i]\n            int position= nums2.size() - 1 - it; //Index of the found element in the original array\n            if(i<=position) ans = max(ans,position-i); //Update distance \n        }\n        return ans;\n        \n    }\n```\n**Feel free to ask any question in the comment section.**\nI hope that you\'ve found the solution useful.\nIn that case, please do upvote and encourage me to on my quest to document all leetcode problems\uD83D\uDE03\n**Happy Coding :)**\n};
[]