DP and Sorting
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: O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n )$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n#similar to VK cup\'22 \u041F\u043E\u0434\u043A\u0440\u0443\u0442\u043A\u0430 II\n\n# Code\n```\nclass Solution {\nprivate:\n    \npublic:\n    int maxSelectedElements(vector<int>& a) {\n        int n=a.size();\n        pair<int,int> dp ={1,1};\n        int ans=1;\n        \n        sort(a.begin(),a.end());\n\n        for(int i=1;i<n;i++){\n            pair<int,int> cur={1,1};\n            if(a[i-1]+1==a[i]){\n              cur.first=max(1+dp.first,dp.second);\n              cur.second=1+dp.second;\n            }\n            else if(a[i-1]==a[i]){\n                cur.first=dp.first;\n                cur.second=max(dp.first+1,dp.second);\n            }\n            else if(a[i-1]+2==a[i]){\n               cur.first=dp.second+1;\n            }\n            ans=max(ans,max(cur.first,cur.second));\n            dp=cur;\n        }\n        \n        return ans;\n    }\n};\n```
['Dynamic Programming', 'Sorting', 'C++']