C++ | Sorting and  DP | As easy as possible
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(nlogn)$$\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```\n\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size();\n\t\tsort(nums.begin(), nums.end());\n\t\tunordered_map<int, int> dp; // length of consecutive sequence starting at arr[i] or arr[i] + 1\n\t\tint ans = 0;\n\t\tfor (int i = n - 1; i >= 0; i--) {\n\t\t\tint x = nums[i];\n\t\t\tauto it = dp.find(x + 1);\n\t\t\tint cur = 1;\n\t\t\tif (it != dp.end()) {\n\t\t\t\tcur += it->second;\n\t\t\t}\n\t\t\tdp[x] = max(dp[x], cur);\n\t\t\tans = max(ans, cur);\n\t\t\tx++;\n\t\t\tcur = 1;\n\t\t\tit = dp.find(x + 1);\n\t\t\tif (it != dp.end()) {\n\t\t\t\tcur += it->second;\n\t\t\t}\n\t\t\tdp[x] = max(cur, dp[x]);\n\t\t\tans = max(ans, cur);\n\t\t}\n\t\treturn ans;\n    }\n};\n\n\n```
['C++']