Simple C++ Solution | DP | Simple Observation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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 hope(int index,int state,int flag,vector<int>&nums,vector<vector<vector<int>>>&dp){\n    if(index>=nums.size())return 0;\n    if(dp[index][state+1][flag]!=-1)return dp[index][state+1][flag];\n    if(state==0){\n        if(flag==0){\n            int prev=nums[index-1];\n            int curr=nums[index];\n            if(curr!=prev) if(curr<prev || curr-prev>1)return 0;\n        }\n        else if(flag==1){\n            int prev=nums[index-1]+1;\n            int curr=nums[index];\n            if(curr!=prev) if(curr<prev || curr-prev>1)return 0;\n        }\n    }\n   else if(state==1){\n        if(flag==0){\n            int prev=nums[index-2];\n            int curr=nums[index];\n            if(curr!=prev) if(curr<prev || curr-prev>1)return 0;\n        }\n        else if(flag==1){\n            int prev=nums[index-2]+1;\n            int curr=nums[index];\n            if(curr!=prev) if(curr<prev || curr-prev>1)return 0;\n        }\n    }\n    int pick=-1e9;\n    int not_pick=-1e9;\n    int one_step=-1e9;\n    int one_step_inc=-1e9;\n    int two_step=-1e9;\n    int two_step_inc=-1e9;\n    int prev=0;\n    if(state==0)prev=nums[index-1];\n    else if(state==1)prev=nums[index-2];\n    if(flag==1)prev=prev+1;\n    if(state==-1){\n     one_step=1+hope(index+1,0,0,nums,dp);\n     one_step_inc=1+hope(index+1,0,1,nums,dp);\n     two_step=1+hope(index+2,1,0,nums,dp);\n     two_step_inc=1+hope(index+2,1,1,nums,dp);\n     not_pick=hope(index+1,state,flag,nums,dp);\n    }\n    else{\n    if(nums[index]-prev==1)one_step=1+hope(index+1,0,0,nums,dp);\n     if(nums[index]-prev==0)one_step_inc=1+hope(index+1,0,1,nums,dp);\n     if(nums[index]-prev==1)two_step=1+hope(index+2,1,0,nums,dp);\n     if(nums[index]-prev==0)two_step_inc=1+hope(index+2,1,1,nums,dp);\n    }\n    pick=max(pick,one_step);\n    pick=max(pick,one_step_inc);\n    pick=max(pick,two_step);\n    pick=max(pick,two_step_inc);\n    return dp[index][state+1][flag]=max(pick,not_pick);\n}\n    int maxSelectedElements(vector<int>& nums) {\n        map<int,int>m;\n        for(int i=0;i<nums.size();i++){\n            if(m.find(nums[i])==m.end())m.insert({nums[i],1});\n            else{\n                auto it=m.find(nums[i]);\n                it->second=it->second+1;\n            }\n        }\n        vector<int>v;\n        for(auto it=m.begin();it!=m.end();it++){\n            if(it->second==1)v.push_back(it->first);\n            else if(it->second>1){\n                v.push_back(it->first);\n                v.push_back(it->first);\n            }\n        }\n        vector<vector<int>>dp1(3,vector<int>(2,-1));\n        vector<vector<vector<int>>>dp(v.size(),dp1);\n        int ans=hope(0,-1,0,v,dp);\n        return ans;\n    }\n};\n```
['Array', 'Hash Table', 'Dynamic Programming', 'Greedy', 'C++']