7 Line c++ | Binary Search | STL
For any element nums1[i], we essentially need to find the farthest element nums2[j] with j>=i such that the nums2[j] element is not less than nums1[i].\n\nA bru
For any element nums1[i], we essentially need 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-decreasing order. \n\nSo, what we instead do is, reverse the second array to make it non-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\nIn the given code, j represents the index from the end (which would be it\'s index in the original array)\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 j = nums2.size() - 1 - it; //Index of the found element in the original array\n            if(i<=j) ans = max(ans,j-i); //Update distance \n        }\n        return ans;\n        \n    }\n};\n```\n\n\nHope this helps!\nStay Safe.
['C', 'Binary Tree']