C++ solution using lower_bound iterator with comments
\nclass Solution\n{\npublic:\n    //to return maximum among two elements\n    int max(int a, int b)\n    {\n        if (a > b)\n            return a;\n        r
```\nclass Solution\n{\npublic:\n    //to return maximum among two elements\n    int max(int a, int b)\n    {\n        if (a > b)\n            return a;\n        return b;\n    }\n    int maxDistance(vector<int> &nums1, vector<int> &nums2)\n    {\n        int dist = 0, n1 = nums1.size(), n2 = nums2.size();\n\n        //sorting nums2 vector in ascending order\n        sort(nums2.begin(), nums2.end());\n\n        for (int i = 0; i < min(n1, n2); i++)\n        {\n            //lower_bound will return refernce to element >= nums[i]\n            auto itr = lower_bound(nums2.begin(), nums2.end() - i, nums1[i]);\n\n            //in case elemt found\n            if (itr != nums2.end() - i)\n            {\n                //as previously sorted in decreasing order\n                int original_index = n2 - 1 - (itr - nums2.begin());\n                dist = max(dist, original_index - i);\n            }\n        }\n        return dist;\n    }\n};\n```
['Sorting', 'Binary Tree', 'Iterator']