DP, O(n), TC
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\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```python3 []\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n        from collections import defaultdict\n        n = len(nums)\n        nums = sorted(nums)\n        tab = [defaultdict(int) for i in range(n)]\n        ans = 1\n        tab[0][nums[0]] = 1\n        tab[0][nums[0] + 1] = 1\n        for i in range(1, n):\n            tab[i][nums[i] + 1] = max(1 + tab[i - 1][nums[i]], tab[i - 1][nums[i] + 1])\n            tab[i][nums[i]] = max(1 + tab[i - 1][nums[i] - 1], tab[i - 1][nums[i]])\n            ans = max(ans, tab[i][nums[i]])\n            ans = max(ans, tab[i][nums[i] + 1])\n        return ans\n```
['Python3']