Maximum Distance Between a Pair of Values LeetCode solution in C++
1class Solution {2public:3int maxDistance(vector<int> &nums1, vector<int> &nums2) {4reverse(nums2.begin(), nums2.end());5int ans = 0;6for (int i = 0; i < nums1.size(); ++i) {7auto it = lower_bound(nums2.begin(), nums2.end(), nums1[i]) -8nums2.begin(); // Finds first element greater than or equal to nums1[i]9int j = nums2.size() - 1 - it; // Index of the found element in the original array10if (i <= j) ans = max(ans, j - i); // Update distance11}12return ans;13}14};