C++ Implementation using Binary Search + Brute Force
Key Points\n- Find the unique segment till the length k using Binary Search\n- Then sort these segments according to the highest length\n- Then Brute Force by P
**Key Points**\n- Find the unique segment till the length k using Binary Search\n- Then sort these segments according to the highest length\n- Then Brute Force by Picking one of the greatest no till 100 times and matching with the other greatest no\n\n# Code\n```\nclass Solution {\npublic:\n    static bool cmp(pair<int,int> &p1,pair<int,int> &p2){\n        return p1.second-p1.first>p2.second-p2.first;    \n    }\n    int maximizeWin(vector<int>& prizePositions, int k) {\n       \n        set<int>st;\n        vector<pair<int,int>>vp;\n        for(int i=0;i<prizePositions.size();i++){\n            if(st.find(prizePositions[i])==st.end()){\n                st.insert(prizePositions[i]);\n                int ind=upper_bound(prizePositions.begin(),prizePositions.end(),prizePositions[i]+k)-prizePositions.begin();\n                // ind--;\n                vp.push_back({i,ind-1});\n                st.insert(prizePositions[i]);\n            }\n        }\n        sort(vp.begin(),vp.end(),cmp);\n        int n=vp.size();\n        int ans=0;\n        for(int i=0;i<min(100,n);i++){\n            pair<int,int>p=vp[i];\n            for(int j=0;j<vp.size();j++){\n                \n                pair<int,int>p1=vp[j];\n                if(p.second>=p1.first && p1.first>=p.first || p1.second>=p.first &&p.first>=p1.first){\n                    int u=min(p.first,p1.first);\n                    int v=max(p.second,p1.second);\n                    ans=max(ans,v-u+1);\n                }\n                else{\n                    ans=max(ans,p.second-p.first+p1.second-p1.first+2);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['Binary Search', 'C++']