C++ SOLUTION WITH UPPER_BOUND + SUFFIX MAXIMUM ARRAY
Time Complexity: O(nlog(n))\n\n\nclass Solution \n{\npublic:\n    int maximizeWin(vector<int>& a, int k) \n    {\n        int n = a.size();\n        vector<pair
# Time Complexity: $$O(nlog(n))$$\n\n```\nclass Solution \n{\npublic:\n    int maximizeWin(vector<int>& a, int k) \n    {\n        int n = a.size();\n        vector<pair<int, int>> b(n), s(n);\n        for (int i = 0; i < n; ++i)\n        {\n            int target = a[i] + k;\n            int idx = max(0, (int)(upper_bound(a.begin(), a.end(), target) - a.begin()) - 1);\n            b[i] = {i, idx};\n        }\n        \n        int mx = 0;\n        pair<int, int> curr;\n        for (int i = n - 1; i >= 0; --i)\n        {\n            if (b[i].second - b[i].first + 1 > mx)\n            {\n                mx = b[i].second - b[i].first + 1;\n                curr = {b[i].first, b[i].second}; \n            }\n            \n            s[i] = curr;\n        }\n        \n        int res = 1;\n        for (int i = 0; i < n; ++i)\n        {\n            int j = b[i].second;\n            \n            res = max(res, b[i].second - b[i].first + 1 + s[j].second - s[j].first + 1 - (s[j].first == b[i].second));\n        }\n        \n        return res;\n    }\n};\n```
['C++']