C++ Intuitive DP
\n# Code\n\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& a) {\n     int n= a.size();\n        sort(a.begin(),a.end());\n        for(auto 
\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& a) {\n     int n= a.size();\n        sort(a.begin(),a.end());\n        for(auto it:a)cout<<it<<" ";cout<<"\\n";\n        vector<vector<int>>dp(n,vector<int>(2,0));\n        dp[0][0]=1;\n        dp[0][1]=1;\n        for(int i=1;i<n;i++){\n            int df=a[i]-a[i-1];\n            if(df>2){dp[i][0]=1;dp[i][1]=1;}\n            else if(df==1){dp[i][0]=dp[i-1][0]+1; dp[i][1]=dp[i-1][1]+1; }\n            else if(df==0){\n                dp[i][0]=dp[i-1][0];\n                dp[i][1]=max(dp[i-1][0]+1,dp[i-1][1]);\n                \n            }\n            else if(df==2){\n                dp[i][0]=dp[i-1][1]+1;\n                dp[i][1]=1;\n            }\n        }\n        int mx=0;\n        for(auto it:dp){\n            \n            mx=max({mx,it[0],it[1]});\n        }\n        return mx;\n    }\n};\n```
['C++']