Sort and greedy, super easy with several lines of code
Intuition\nJust need to sort and count.\nTry to increase the element to find the consecutive elements first\nI\'m really new to python so the code of i13e is de
# Intuition\nJust need to sort and count.\nTry to increase the element to find the consecutive elements first\nI\'m really new to python so the code of i13e is definitely better\n# Approach\n<!-- Describe your approach to solving the problem. -->\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:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n        nums = sorted(nums)\n        counts = {}\n        for num in nums:\n            counts[num + 1] = counts.get(num, 0) + 1\n            counts[num] = 1 + counts.get(num - 1, 0)\n            \n        ans = 0\n        for key, value in counts.items():\n            ans = max(ans, value)\n            \n        return ans\n            \n```
['Python3']