Using Binary Search || Easy To understand 🌟
Approach\nFind the Most appropriate value from all index\'s and pick that value and maximum of remaining values as answer.\nFind same thing for all 1<=i<=n.\n\n
# Approach\nFind the Most appropriate value from all index\'s and pick that value and maximum of remaining values as answer.\nFind same thing for all 1<=i<=n.\n\n# Complexity\n- Time complexity:\n  O(N LogN)\n\n- Space complexity:\n  O(2N)\n\n# Code\n```\nclass Solution {\npublic:\n    int lower(vector<int> &v,int val) {\n        int i=0,j=v.size()-1,ans=-1;\n        while(i<=j) {\n            int mid=(i+j)/2;\n            if(v[mid]<=val) {\n                ans=mid;\n                i=mid+1;\n            }\n            else j=mid-1;\n        } return ans;\n    }\n\n    int maximizeWin(vector<int>& p, int k) {\n        int n = p.size();\n        vector<int> ans(n);\n        for(int i=0 ; i<n ; ++i) {\n            ans[i]=lower(p,p[i]+k)-i+1;\n        }\n        vector<int> b(n);\n        for(int i=n-1 ; i>=0 ; --i) b[i] = max((i==n-1?0:b[i+1]),ans[i]);\n        int res=0;\n        for(int i=0 ; i<n ; ++i) {\n            int extra=0;\n            if(i+ans[i]<n) {\n                extra=b[i+ans[i]];\n            }\n            res = max(res,ans[i]+extra);\n        }\n        return res;\n    }\n};\n```
['Binary Search', 'Dynamic Programming', 'C++']