C++ easy solution O(m*logn)
\n# Algorithm\nInitialize answer = 0.\nIterate over one array (let\'s say nums1), for each number nums1[i], we use binary search to find the insertion position 
\n# Algorithm\nInitialize answer = 0.\nIterate over one array (let\'s say nums1), for each number nums1[i], we use binary search to find the insertion position j of nums1[i] to nums2.\nIf j < i, we move on to the next i by repeating the step 2.\nOtherwise, we find one valid pair, update answer as answer = max(answer, j - i).\n# Code\n```\nclass Solution {\npublic:\n    int maxDistance(vector<int>& nums1, vector<int>& nums2) {\n        int m = int(nums1.size()), n = int(nums2.size());\n        int ans = 0;\n        for (int i = 0; i < m; ++i) {\n            int k = nums2.rend() - lower_bound(nums2.rbegin(), nums2.rend(), nums1[i]);\n            if (k > 0) {\n                ans = max(ans, k - i - 1);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']