DP C++ solution ✔️✔️✔️
Intuition\nDynamic Programming\n\n# Approach\n1. Sort the array.\n2. Calculate the max length we can make for each number in the array if it remain same and if 
# Intuition\nDynamic Programming\n\n# Approach\n1. Sort the array.\n2. Calculate the max length we can make for each number in the array **if it remain same** and **if we increase it for once**.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& a) {\n        int n = a.size(), ans = 1;\n        sort(a.begin(), a.end());\n        map<int, int> mp;\n        for(auto it: a)\n        {\n            mp[it+1] = max(mp[it+1], 1+mp[it]);\n            mp[it] = max(mp[it], 1+mp[it-1]);\n            ans = max({ans, mp[it], mp[it+1]});\n        }\n        return ans;\n    }\n};\n```
['Array', 'Dynamic Programming', 'Sorting', 'C++']