Detailed solution with comments | Sliding Window | Prefix Sum | Suffix Sum | O(N)
\nclass Solution\n{\n    public:\n       \t// basically for ant index i, (0,i) will give me one segment out of all segments possible\n       \t//in this range (
```\nclass Solution\n{\n    public:\n       \t// basically for ant index i, (0,i) will give me one segment out of all segments possible\n       \t//in this range (0,i) in such a way that it will give me max. prize. Similarly (i+1,n-1)\n       \t// will give me one segment out of all segments possible in this range (i+1,n-1) in such\n       \t// way that it will give me max.prize. So adding up their values will give me max.prize\n       \t// possible for any index i and i have to do that for every index. out of thsese whichever \n       \t// will give maximum answer, would be my answer.\n        int maximizeWin(vector<int> &prizePositions, int k)\n        {\n            int n = prizePositions.size();\n            vector<int> pref(n), suff(n);\n```\n\n           \t// Find maximum count of prize for(0,i). It can be[1,3], [2,4], [3,5]...out of these\n           \t// segments whichever will give me max count, i will choose it. For this example it \n           \t// is[1,3]\n\n            int count = 0, maxCount = 0;\n            int st = 0;\n            int end = prizePositions[0] + k;\n\n            for (int i = 0; i < n; i++)\n            {\n                int curr = prizePositions[i];\n\n                if (curr <= end) count++;\n                else\n                {\n                   \t// i will keep contracting my window until my curr is in range or so to say\n                   \t// find a new range\n                    while (curr > end)\n                    {\n                        st++;\n                        end = prizePositions[st] + k;\n                        count--;\n                    }\n\n                    count++;\n                }\n                maxCount = max(maxCount, count);\n                pref[i] = maxCount;\t// store the max count for (0,i);\n            }\n\n            count = 0, maxCount = 0;\n            st = n - 1;\n            end = prizePositions[n - 1] - k;\n\n            for (int i = n - 1; i >= 0; i--)\n            {\n                int curr = prizePositions[i];\n\n                if (curr >= end) count++;\n                else\n                {\n                   \t// i will keep contracting my window until my curr is in range or so to say\n                   \t// find a new range\n                    while (curr < end)\n                    {\n                        st--;\n                        end = prizePositions[st] - k;\n                        count--;\n                    }\n\n                    count++;\n                }\n                maxCount = max(maxCount, count);\n                suff[i] = maxCount;\t// store the max count for (i,n-1)\n            }\n\n            int maxPrize = 0;\n            for (int i = 0; i < n; i++)\n            {\n                int prize = pref[i];\n                if (i + 1 < n) prize += suff[i + 1];\n                maxPrize = max(maxPrize, prize);\n            }\n            return maxPrize;\n        }\n};\n\n**If you find this explanation helpful, make sure you upvote this!**\n**For more such solution, you can follow this:** https://github.com/tanmoyheritage23/Leetcode-GFG\n**Find me on LinkedIn:** https://www.linkedin.com/in/tanmoysahaofficial/
['Prefix Sum']