Sliding window O(N)
\n\n# Code\n\nclass Solution {\npublic:\n    int maximizeWin(vector<int>& v, int k) {\n        int n=v.size(),ans=0;\n\n     // sliding window from right\n     
\n\n# Code\n```\nclass Solution {\npublic:\n    int maximizeWin(vector<int>& v, int k) {\n        int n=v.size(),ans=0;\n\n     // sliding window from right\n        int cnt=0,j=n-1;\n        vector<int> res(n+2,0);\n        for(int i=n-1;i>=0;i--){\n           cnt++;\n           while(v[j]-v[i]>k){ \n               j--;\n               cnt--;\n           }\n           res[i]=max(res[i+1],cnt);\n        }\n\n   // sliding window from left\n       j=0,cnt=0;\n        for(int i=0;i<n;i++){\n            cnt++;\n            while(v[i]-v[j]>k){\n                j++;\n                cnt--;\n            }\n            ans=max(ans,cnt+res[i+1]);\n        }\n        return ans;\n    }\n};\n```
['C++']