dp  || similar to buy and sell stocks with 2 transaction || memoization || binary search
Intuition\n Describe your first thoughts on how to solve this problem. \nwe can use dp to find at most 2 different ranges and return the maximum sum of these ra
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can use dp to find at most 2 different ranges and return the maximum sum of these ranges. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe limit ourself to 2 picks .\nwe use the principal of take / not take at a given index.\nwe can take a starting element from the array , then using the  fact that array is sorted , we can use binary search to find the last index of range such that , the element is less than or equal to first element +k.\n\n\nwe can do this process at most 2 times.\nthen return maximum number of elements accumulated.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(2*n*log(n))\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(2n)\n# Code\n```\nclass Solution {\npublic:\n // this will return index of the last valid element in the range\n \n int search(vector<int>& prize,int s, int key){\nint e=prize.size()-1;\nint mid;\nint ans=-1;\nwhile(s<=e){\n\nmid=(s+e)/2;\nif(prize[mid]<=key){\n    ans=mid;\n    s=mid+1;\n}\nelse\n e=mid-1;\n\n}\nreturn ans;\n } \n\nint solve(vector<int>& prize, int &k,int idx,int t,int &n,vector<vector<int>>&dp){\n\nif(t==2) // we have used 2 ranges/transactions  \nreturn 0;\n\nif(idx>=n) // we are out of the array\nreturn 0;\n\nif(dp[idx][t]!=-1)\nreturn dp[idx][t];\n// memoization\nint l=0,r=0;\n// take \nint range = search(prize,idx,prize[idx]+k); \nl= range-idx+1 + solve(prize,k,range+1,t+1,n,dp);\n// not take\nr=solve(prize,k,idx+1,t,n,dp);\n\nreturn dp[idx][t]=max(l,r);\n}\n\n    int maximizeWin(vector<int>& prize, int k) {\n        //take, not take\n        // we can take at most 2 times\n\nint n=prize.size();\nvector<vector<int>>dp(n,vector<int>(2,-1));\n// n size of all the indexes from 0- (n-1) and 2 size for atmost 2 transactions.\nreturn solve(prize,k,0,0,n,dp);\n\n    }\n};\n```
['Binary Search', 'Dynamic Programming', 'Recursion', 'Memoization', 'C++']