dp+recursion
Intuition\n\n# Approach\n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n Add your space complexity here,
# Intuition\n\n# Approach\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nint bin(int indx,int num,vector<int>&v){\n    int lo =indx+1;int hi =v.size()-1;\n    while((hi-lo)>1){\n        int mid =(hi+lo)/2;\n        if(v[mid]>=num){\n            hi=mid;\n        }\n        else {\n            lo=mid+1;\n        }\n    }\n    if(v[lo]==num){\n        return lo;\n    }\n    else if(v[hi]==num){\n        return hi;\n    }\n    else{\n        return -1;\n    }\n}\nint f(int indx,vector<int>&v,int ext, vector<vector<vector<int>>>&dp,int state){\n    if(indx==v.size()-1){\n        return 0;\n    }\n    if(dp[indx][ext][state]!=-1){\n        return dp[indx][ext][state];\n    }\n    int ans =0;\n    int next=bin(indx,v[indx]+ext,v);\n    if(next!=-1){\n        ans=1+f(next,v,1,dp,1);\n    }\n    else {\n        int n1=bin(indx,v[indx]+ext+1,v);\n        if(n1!=-1){\n            ans=1+f(n1,v,0,dp,state);\n        }\n        else{\n           if(state==1){\n             return 0;\n           }\n           else {\n               int n2 =bin(indx,v[indx]+2,v);\n               if(n2!=-1){\n                ans=1+f(n2,v,0,dp,1);\n               }\n               else {\n                   return 0;\n               }\n           }\n        }       \n    }\n    return dp[indx][ext][state]=ans;\n    \n}\n    int maxSelectedElements(vector<int>& v) {\n    sort(v.begin(),v.end());\n\n    int ans =0;\n    vector<vector<vector<int>>>dp(v.size()+1,vector<vector<int>>(2,vector<int>(2,-1)));\n    for(int i =0;i<v.size();i++){\n      \n        ans=max(ans,1+f(i,v,0,dp,0));\n    }\n    return ans;\n        \n    }\n};\n```
['Recursion', 'Memoization', 'C++']