DP easy
Intuition\n Describe your first thoughts on how to solve this problem. \nNeeds to think about the impact of incrementing and not incrementing the current elemen
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNeeds to think about the impact of incrementing and not incrementing the current element to answer.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- max({ans,mp[it - 1] + 1, mp[it] + 1})\n  This will calulate best of (ans, not incrementing current element and incrementing current element)\n- mp[it + 1] = mp[it] + 1;\n  This is for storing the longest consecutive incresing element which ends at it+1\n- mp[it] = mp[it - 1] + 1;\n  This is for storing the longest consecutive incresing element which ends at it\n\n> Order of calculating mp[it] and mp[it+1] is important\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& a) {\n        int n = a.size();\n        sort(a.begin(), a.end());\n        int ans = 0;\n        unordered_map<int, int> mp;\n        for (auto it : a) {\n            ans = max({ans,mp[it - 1] + 1, mp[it] + 1});\n            mp[it + 1] = mp[it] + 1;\n            mp[it] = mp[it - 1] + 1;\n        }\n        return ans;\n    }\n};\n```
['C++']