[Java] Prefix MAX + Binary Search, O(nlogn) solution
\nclass Solution {\n    public int maximizeWin(int[] a, int k) {\n        int max_till_here=0;\n        int maxans=0;\n        for(int j=0;j<a.length;j++)\n    
```\nclass Solution {\n    public int maximizeWin(int[] a, int k) {\n        int max_till_here=0;\n        int maxans=0;\n        for(int j=0;j<a.length;j++)\n        {\n            int end=j;\n            int l=0;\n            int r=a.length-1;\n            while(l<=r)\n            {\n                int mid=l+(r-l)/2;\n                if(a[mid]<=a[j]+k)\n                {\n                    end=mid;\n                    l=mid+1;\n                }\n                else\n                    r=mid-1;\n            }\n            maxans=Math.max(maxans,max_till_here+(end-j)+1);\n            int start=j;\n            l=0;\n            r=a.length-1;\n            while(l<=r)\n            {\n                int mid=l+(r-l)/2;\n                if(a[mid]>=a[j]-k)\n                {\n                    start=mid;\n                    r=mid-1;\n                }\n                else\n                    l=mid+1;\n            }\n            max_till_here=Math.max(max_till_here,(j-start)+1);\n        }\n        return maxans;\n    }\n}\n```
['Binary Search', 'Sliding Window']