C++ DP || Beats 100%RT || Beats 100% Memory
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```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size();\n        sort(nums.begin(),nums.end());\n        \n        int result = 1;        \n        vector<vector<int>> dp(n,{1,1});\n        \n        for(int i = n-2; i >= 0; i--){\n            if(nums[i+1] - nums[i] == 1){\n                dp[i][0] = max(dp[i][0], dp[i+1][0] + 1);\n                dp[i][1] = max(dp[i][1], dp[i+1][1] + 1);\n            }\n            if(nums[i+1] == nums[i]){\n                dp[i][0] = max(dp[i][0], dp[i+1][1] + 1);\n                dp[i][0] = max(dp[i][0], dp[i+1][0]);\n                dp[i][1] = max(dp[i][1], dp[i][1]);\n            }\n            if(nums[i+1] - nums[i] == 2){\n                dp[i][1] = max(dp[i][1], dp[i+1][0] + 1);\n            }\n            \n            result = max(result,max(dp[i][0],dp[i][1]));\n        }\n        \n        return result;\n    }\n};\n```
['C++']