[c++] dp simple solution | similar to longest increasing subsequence | editorial
If the numbers are negetive or very large a map can be used instead of dp array \n\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\
If the numbers are negetive or very large a map can be used instead of dp array \n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        map<int, int> dp;\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        \n        int ans = 0;\n        for(int i=(n-1); i>=0; i--) {\n            int cur = nums[i];\n            dp[cur] = max(dp[cur], 1+dp[cur+1]);\n            dp[cur+1] = max(dp[cur+1], 1+dp[cur+2]);\n            \n            \n            ans = max({ans, dp[cur+1], dp[cur]});\n        }\n        \n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C']