Very Simple Code || Explained
Complexity\n- Time complexity: O(n*log(n)) \n\n- Space complexity: O(n) \n\n# Code\n\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) 
# Complexity\n- Time complexity: $$O(n*log(n))$$ \n\n- Space complexity: $$O(n)$$ \n\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(),nums.end()); //sort the given array\n        unordered_map<int,int> m;\n        int maxi = 0;\n        for(auto it : nums) {\n            m[it+1] = max(m[it+1],1+m[it]); //max consecutive elements till (it+1) = 1 + (max consecutive elements till it)\n            m[it] = max(m[it],1+m[it-1]); //max consecutive elements till (it) = 1 + (max consecutive elements till (it-1))\n            maxi = max({maxi,m[it],m[it+1]}); //take max of all\n        }\n        return maxi;\n    }\n};\n```
['Hash Table', 'Sorting', 'C++']