Unique Solution with two HashMaps and Intution
Intuition\ntake previous count of consectuive elements as it might be used if next element is prev+2 and it would be counted in answer.\n\n# Approach\ntwo hasma
# Intuition\ntake previous count of consectuive elements as it might be used if next element is prev+2 and it would be counted in answer.\n\n# Approach\ntwo hasmap \nONe for iteration and one for checking if element was present in original array.\n\n# Complexity\n- Time complexity:\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```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        map<int,int>mp;\n        unordered_map<int,int>mp1;\n        for(auto i:nums){\n            mp[i]++;\n            mp1[i]++;\n        }\n        int ans=0;\n        int count=0;\n        int prev=-1;\n        int pure=0;\n        while(!mp.empty()){\n            count=0;\n            while(!mp.empty()){\n                auto i=mp.begin();\n                if(prev+1!=i->first){\n                    if(prev+2==i->first&&mp1.count(prev)){\n                        count=1+pure;\n                        pure=0;\n                    }\n                    else{\n                        count=1;\n                        pure=0;\n                    }\n                    if(mp1.count(i->first)){\n                        pure++;   \n                    }\n                    if(i->second>1){\n                        mp[i->first+1]+=1;\n                    }\n                    prev=i->first;\n                    ans=max(ans,count);\n                }\n                else if(prev+1==i->first){\n                    if(i->second>1){\n                        mp[i->first+1]+=1;\n                    }\n                    if(mp1.count(i->first)){\n                        pure++;\n                    }\n                    else{\n                        pure=0;\n                    }\n                    count++;\n                    prev+=1;\n                    ans=max(ans,count);\n                }\n                //cout<<i->first<<" "<<i->second<<" "<<count<<endl;\n                mp.erase(i);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']