Beats 100%  | Most Intuitive Approach with Clear Explanation
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- How can we optimally check the maxSize of the sequence starting from each possi
![image.png](https://assets.leetcode.com/users/images/b67beace-91b8-4191-ba21-b9f5fb17e19f_1708238970.3458006.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- How can we optimally check the maxSize of the sequence starting from each possible values.\n- And possible starting values can be $$nums[i]$$, $$ nums[i] + 1$$.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort $$nums$$.\n2. Maintain a Hash-Map that stores the maxSize of a sequence starting from a value (i.e. Key in Hash-Map).\n2. Start from largest element in $$nums$$ and check what largest sequence can be formed starting from $$nums[i]$$ and $$nums[i] + 1$$\n3. Update Hash-Map for these values as well.\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxSelectedElements(vector<int>& nums) {\n        int n = nums.size();\n        sort(nums.begin(), nums.end());\n\n        unordered_map<int, int>maxFor;\n        int maxSize = 0;\n        for(int i = n - 1; i >= 0; --i){\n            if(maxFor.find(nums[i] + 1) == maxFor.end()){\n                maxFor[nums[i]] = 1;\n            }\n            else{\n                maxFor[nums[i]] = 1 + maxFor[nums[i] + 1];\n            }\n\n            if(maxFor.find(nums[i] + 2) == maxFor.end()){\n                maxFor[nums[i] + 1] = 1;\n            }\n            else{\n                maxFor[nums[i] + 1] = 1 + maxFor[nums[i] + 2];\n            }\n\n            maxSize = max(maxSize, maxFor[nums[i]]);\n            maxSize = max(maxSize, maxFor[nums[i] + 1]);\n        }\n        return maxSize;\n    }\n};\n```
['Hash Table', 'Dynamic Programming', 'Greedy', 'Ordered Map', 'Sorting', 'C++']