C++ Easy Solution
Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(n)\n# Code\n\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& 
# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        ranges::sort(nums);\n        map<int, int> m;\n        int res = 0;\n        for(int& num: nums){\n            m[num+1] = max(m[num+1], m[num] + 1);\n            m[num] = max(m[num], m[num-1] + 1);\n            res = max(res, max(m[num+1], m[num]));\n        }\n        return res;\n    }\n};\n```
['Ordered Map', 'C++']