c++ solution
\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        vecto
```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        vector<int>dp(nums.back()+3,0);\n        int ans=0;\n        for(int i=n-1;i>=0;i--)\n        {\n            dp[nums[i]]=max(dp[nums[i]+1]+1,dp[nums[i]]);\n            dp[nums[i]+1]=max(dp[nums[i]+2]+1,dp[nums[i]+1]);\n            ans=max({ans,dp[nums[i]],dp[nums[i]+1]});\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C', 'Sorting', 'C++']