c++, 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 {\n    private:\n    int have[1000003],mp[1000004];\n    // dp[i][0/1] = dp[i] max val where lift kora jabe or jabena\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n         int n = nums.size();\n         sort(nums.begin(),nums.end());\n         int dp[n+5][2]; // 0-> canot lift //  1-> can lift\n         int ans = 1;\n         for(int i = 0;i<n;i++){\n            if(i == 0){\n                dp[i][0] = 1;\n                dp[i][1] = 1;\n                continue;\n            }\n            if(nums[i] == nums[i-1]){\n                dp[i][0] = dp[i-1][0];\n                dp[i][1] = 1+dp[i-1][0]; // check later\n            }\n            else if(nums[i] == nums[i-1]+1){\n                dp[i][0] = dp[i-1][0]+1;\n                dp[i][1] = max(1,dp[i-1][1]+1);\n            }\n            else if(nums[i] == nums[i-1]+2){\n                dp[i][0] = max(1,1+dp[i-1][1]);\n                dp[i][1] = 1;\n            }\n            else{\n                dp[i][0] = 1;\n                dp[i][1] = 1;\n            }\n            ans = max(ans,max(dp[i][0],dp[i][1]));\n\n         }\n         \n         return ans;\n        \n    }\n};\n```
['C++']