C++ dynamic programming
\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        unordered_map<int, int> m;\n    
```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        unordered_map<int, int> m;\n        int res = 1;\n        for (int i = 0; i < nums.size(); ++i) {\n            res = max(res, m[nums[i] + 1] = m[nums[i]] + 1);\n            res = max(res, m[nums[i]] = m[nums[i] - 1] + 1);\n        }\n        return res;\n    }\n};\n```
[]