Easy DP
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```cpp []\nclass Solution {\npublic:\n\n    int maxSelectedElements(vector<int>& nums) {\n\n        sort(nums.begin(),nums.end());\n\n        int n = nums.size();\n        unordered_map<int, int>dp;\n        int ans = 0;\n\n        for(int i=0;i<n;i++) {\n            int ele = nums[i];\n            dp[ele + 1] = dp[ele] + 1;\n            dp[ele] = dp[ele - 1] + 1;\n\n            ans = max(ans, dp[ele + 1]);\n            ans = max(ans, dp[ele]);\n        }\n\n        return ans;\n        \n    }\n};\n```
['C++']