C++ - Greedy solution
\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int start=0,ans=1,offset=0,lastNu
```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int start=0,ans=1,offset=0,lastNum=nums[0];\n        for(int i=1;i<nums.size();i++){\n            if (nums[i]-lastNum>1){\n                int j=i-1,t=nums[i]-2;\n                offset=0;//offset is used to find number of prev numbers which we can align with a new sequence\n                while(j>=start && nums[j]>=t){\n                    if (nums[j]==t) t--,offset++;\n                    j--;\n                }\n                \n                start=i;\n                lastNum=nums[i];\n            }\n            else if (lastNum>nums[i]) continue;\n            else if (nums[i]==lastNum){\n                lastNum++;\n            }\n            else{\n                lastNum=nums[i];\n            }\n            ans = max(ans,lastNum-nums[start]+1+offset);\n        }\n        return ans;\n    }\n};\n```
[]